【问题标题】:help needed with a jquery word counter (adding maximum words)jquery单词计数器需要帮助(添加最大单词)
【发布时间】:2010-11-25 12:31:59
【问题描述】:

我需要一些帮助来将 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>

任何帮助将不胜感激:)

【问题讨论】:

    标签: jquery limit max word-count


    【解决方案1】:

    让用户停止的唯一方法是this.value = this.value.substr(0,100); 然而,这是一种糟糕的用户体验,使用户难以打字。

    考虑在点击提交时阻止用户发送内容。例如,Twitter 就是这样做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多