2012-08-07 37 views
4

我無法弄清楚爲什麼我的JavaScript不能正常工作。它與j Query以及它應該選擇所有的文本區域標籤,然後爲每個文本區域統計輸入的字符數量,然後不允許用戶在達到最大長度後再輸入字符數量,還顯示字符計數器在每個文本區域框下。 它所做的只是顯示剩餘的字符,但在按下每個鍵後不會遞減,並且在達到最大長度時也不會停止。它也不會選擇所有的文本區域,只需要找到它的第一個區域。使用jQuery的TextArea字符計數器

這裏是我的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文件中,該文件是我的頁面製作的,並且有我的文本區域。 我還爲textarea元素添加了一個id和maxlength屬性。 任何幫助將是偉大的謝謝你。

+0

噢,抱歉,這只是在問題 – Sheldor 2012-08-07 04:53:09

+0

注意一個錯字是截斷的字符串返回允許的最大長度爲用戶類型是非常笨重 - 更好的改變背景顏色或以其他方式將其突出顯示爲錯誤並將其留給用戶修復。否則當用戶嘗試編輯字段的開始或中間時,用戶會感到困惑,並開始刪除_end _...中的多餘字符。 – nnnnnn 2012-08-07 04:56:47

回答

1

哦,天哪 - 唐諾爲什麼你需要以上,如果你能做到這一點的代碼:http://jsfiddle.net/btyCz/

有限公司演示http://jsfiddle.net/jnXEy/(我已經將限制設置爲20)隨意玩耍。

請讓我再知道如果我錯過了anythig,但低於應該幫助:)

您可以始終把支票上的長度進行限制。

代碼

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

HTML

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

什麼是正則表達式替代?我不認爲它正在做你想做的事 - 在你的小提琴中輸入「asdf {sdf} asdf」',看看計數是什麼...... – nnnnnn 2012-08-07 04:52:45

+0

replace(/{.*}/)的用法是什麼('#charCount')。text(this.value.replace(/{.*}/ g,'').length);' – 2012-08-07 04:54:34

+1

@nnnnnn saweet bruv! ':)'你好嗎?我將盡快替換替換':)' – 2012-08-07 04:54:44

0

你的代碼中有許多語法錯誤。 現在試試這個:

$(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

+0

非常感謝你:)但你可以顯示說哪些行號有語法錯誤,因爲我沒有看到他們。 (我確實在var $ texts = $('textarea [maxlength]')中有額外的單引號;在我的解釋中,我解決了這個問題,再次感謝 – Sheldor 2012-08-07 05:18:55

+0

我更新了代碼,據我記憶。 – 2012-08-07 05:32:30

+0

嗯,所有這些只是我在這個問題上做的愚蠢的拼寫錯誤,實際上並不是我的代碼,但我猜它與它的反應與我的jsp頁面有關,因爲它似乎對你有用。它也可以在一個頁面上爲多個textarea工作嗎?它只會嘗試爲我的jsp頁面中的第一個文本區域元素工作 – Sheldor 2012-08-07 06:02:28