2016-03-04 70 views
2

我试图限制summernote中允许的字数。如何限制summernote中textarea的字数

例如,我只想限制为50个字。

你有什么想法吗?

$(document).ready(function() { 
    $('#summernote').summernote({ 
     height: 20, 
    }); 
}); 

回答

-1

词VS特点: 我建议你上的字符,而不是单词的数量决定,因为这是即使用户轻松,一致。

Summernote创建contenteditable div象下面这样:
enter image description here

有了这方面的知识,并使用下面的答案,就可以实现一个解决方案,你可以限制的字符数: Limiting Number of Characters in a ContentEditable div

2

以为我会贡献到这个职位,因为这有助于我达到我想要的解决方案。似乎建议的答案尚未被接受。

我为maxLength创建了一个变量,以下是我如何配置summernote回调。请注意,我正在使用的summernote版本的编辑区域为.note-editable。下面的解决方案是打字稿,我有一个跨度,我目标显示剩余的字符数。

callbacks: { 
onKeydown(e) { 

    // The text method will ignore HTML tags and preserve non-breaking 
    // space allowing for a true character count 

    let content: string = $('.note-editable').text(); 

    // This checks if the character is alphanumeric (i.e. not a backspace or return key) 

    let isCharacter: boolean = (/[a-zA-Z0-9-_ ]/.test(String.fromCharCode(e.keyCode))); 

    // If the current content length is at max, preventDefault will disallow 
    // any more characters 

    if (isCharacter) { 
    if (content.length >= maxLength) { 
     e.preventDefault(); 
    } 
    } 
}, 
onKeyup(e) { 

    // After the result of checking the character and max length exceeded, 
    // take the resulting count and determine and display characters remaining 

    let content: string = $('.note-editable').text(); 
    let charactersRemaining: number = maxLength - content.length; 
    $('#SomeDivId span').text(charactersRemaining); 
}