使用jQuery的TextArea字符计数器

TextArea character counter using jQuery

本文关键字:字符 计数器 TextArea jQuery 使用      更新时间:2023-09-26

我很难弄清楚为什么我的JavaScript不能正常工作。它的jQuery和它应该选择所有的文本区域标记,然后为每个文本区域计算键入的字符数,然后在达到最大长度后不允许用户再输入,还在每个文本区域框下显示一个字符计数器。它所做的只是显示剩余的字符,但在每次按键后不会递减,而且在达到最大长度时也不会停止。它也不会选择所有的文本区域,只会选择找到的第一个区域。

这是我的TextAreaCounter.js

$(document).ready(function){
var $texts = $('textarea[maxlength]');
$texts.each(function){
  $var $this = $(this),
  max = $(this).attr('maxlength'),
  textId = $(this)attr.('id'),
  $parent = $this.parent(),
  countId = textId+'-count';
$div = $('<div>Characters Remaining: </div>').addClass('count-down').insertAfter($this);
$input = $('<input></input>').attr({
    type:"text",
    readOnly: "readOnly",
    value: max,
    id: countId
    }).css({
      width: "25px"
      marginTop: "5px"
      marginBottom: "10px"
    }).addClass('readOnly').appendTo($div);
$this.on({
  keyup: function(){
    val = $this.val(),
    countVal = $('#'+countId).val(),
    if(val.length > max){ 
      $this.val(val.substr(0,max));
      alert('Max length exceeded: ' +max);
      return false;
    }else{
      $('#'+countId).val(max-val.length);
    }
  },
  blur: function(){
    val=$this.val();
    if(val.length > max){
      $this.val(val.substr(0,max));
      alert('Max length exceeded: ' +max);
      return false;
    }else{
      $('#'+countId).val(max-val.length);
    }
  }
});
});
});  

当我添加一些警报时,这里的代码中没有显示,这表明它没有进入带有keyup事件的this.on部分。我将这个外部js文件包含在一个jsp文件中,我的页面就是在这个jsp文件中创建的,其中有我的文本区域。我还向textarea元素添加了一个id和maxlength属性。任何帮助都将不胜感激。

哦,天哪,如果你能做到这一点,为什么你需要上面的代码:http://jsfiddle.net/btyCz/

有限演示http://jsfiddle.net/jnXEy/(我已将限制设置为20)可以随意玩耍。

如果我错过了任何东西,请告诉我,但下面的内容应该有助于:)

你可以随时检查长度来限制它。

代码

$('#myInput').keyup(function() {
    $('#charCount').text( this.value.replace(/{.*}/g, '').length );
});​

HTML

<textarea id="myInput"></textarea> <br>
Counter: <span id="charCount"></span>​

您应该通过

http://www.mediacollege.com/internet/javascript/form/limit-characters.html

限制文本区域中的字符

限制charecter数量的一个简单方法是

<textarea maxlength="50">
Enter text here
</textarea>

欲了解更多数据,请访问

http://www.w3schools.com/html5/att_textarea_maxlength.asp

您的代码有许多语法错误。现在试试这个:

$(document).ready(function () { // bracket was missing here...
    var $texts = $('textarea[maxlength]');
    $texts.each(function () { // bracket was missing here...
        var $this = $(this), // incorrect variable declaration here...
        max = $this.attr('maxlength'),
        textId = $this.attr('id'), // incorrect method call here...
        $parent = $this.parent(),
        countId = textId + '-count',
        $div = $('<div>Characters Remaining: </div>').addClass('count-down').insertAfter($this),
        $input = $('<input></input>').attr({
            type: "text",
            readOnly: "readOnly",
            value: max,
            id: countId
        }).css({
            width: "25px", // missing comma here...
            marginTop: "5px", // missing comma here...
            marginBottom: "10px"
        }).addClass('readOnly').appendTo($div);
        $this.on({
            keyup: function () {
                var val = $this.val(),
                countVal = $('#' + countId).val(); // must have semicolon here...
                if (val.length > max) {
                    $this.val(val.substr(0, max));
                    alert('Max length exceeded: ' + max);
                    return false;
                } else {
                    $('#' + countId).val(max - val.length);
                }
            },
            blur: function () {
                var val = $this.val();
                if (val.length > max) {
                    $this.val(val.substr(0, max));
                    alert('Max length exceeded: ' + max);
                    return false;
                } else {
                    $('#' + countId).val(max - val.length);
                }
            }
        });
    });
});

http://jsbin.com/uzohuv/3