jquery 单词计数器需要帮助(添加最大单词数)
我需要一些帮助来将 max_word 值添加到我发现的 jquery 单词计数器(http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html)
我已经修改了上面的内容稍微编码一下,因为我有多个需要限制的文本区域,但我不确定如何让文本区域在用户输入 100 个单词后停止。
这是代码:
jQuery.fn.wordCount = function(params){
var p = {
counterElement: "display_count"
};
var total_words;
if(params) {
jQuery.extend(p, params);
}
//for each keypress function on text areas
this.keypress(function() {
total_words = this.value.split(/[\s\.\?]+/).length;
jQuery(this).parent().find("."+p.counterElement).html(total_words);
});
};
jQuery(".word_count").wordCount();
<textarea name="question_1" id="question_1" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
<textarea name="question_2" id="question_2" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
<textarea name="question_3" id="question_3" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
任何帮助将不胜感激:)
I need some help with adding a max_word value to a jquery word counter that I found (http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html)
I've modified the above code slightly as I have multiple textareas which need to be limited, but I'm not sure how to make the textarea stop after the user has inputted say 100 words.
Here's the code:
jQuery.fn.wordCount = function(params){
var p = {
counterElement: "display_count"
};
var total_words;
if(params) {
jQuery.extend(p, params);
}
//for each keypress function on text areas
this.keypress(function() {
total_words = this.value.split(/[\s\.\?]+/).length;
jQuery(this).parent().find("."+p.counterElement).html(total_words);
});
};
jQuery(".word_count").wordCount();
<textarea name="question_1" id="question_1" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
<textarea name="question_2" id="question_2" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
<textarea name="question_3" id="question_3" class="word_count"></textarea><div class="counter">Total words: <span class="display_count">0</span></div>
Any help would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让用户停止的唯一方法是执行
this.value = this.value.substr(0,100);
然而,这是一种糟糕的用户体验,导致用户难以打字。
请考虑在单击“提交”时阻止用户发送内容。例如,Twitter 就是这样做的。
The only way to make user stop is to do
this.value = this.value.substr(0,100);
However, this is bad user experience that makes user hard to type.
Consider blocks user from sending the content when clicking submit instead. Twitter do that, for example.