jquery 单词计数器需要帮助(添加最大单词数)

发布于 2024-10-04 11:52:25 字数 1228 浏览 0 评论 0原文

我需要一些帮助来将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-10-11 11:52:25

让用户停止的唯一方法是执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文