如何在同一网页上有多个字符计数,而每个字符数都没有相互影响?

时间:2015-11-12 07:11:15

标签: javascript jquery

如何让字符数影响同一网页上的不同textarea字段,而不会影响彼此的字符数。我的textarea字段的ID为#description,另一个ID为#another-description

HTML

<li><textarea name="description" id="description"></textarea>
<div><span class="character-count"></span></div></li>

<li><textarea name="another_description" id="another-description"></textarea>
<div><span class="character-count"></span></div></li>

Jquery的

$(document).ready(function() {  
    var max = 4000;
    $('.character-count').text('4000 characters left');
    $('#description')
    .keydown(function(event){
        if (event.keyCode != 8 && event.keyCode != 46 && $(this).val().length >= max) {
            event.preventDefault();
        }
    })
    .keyup(function(){
        var $this = $(this),
            val = $this.val().slice(0, max),
            val_length = val.length,
            left = max - val_length;

        $('.character-count').text(left + ' characters left');

        $this.val(val);
    });
});

3 个答案:

答案 0 :(得分:2)

您可以创建一个函数,将每个变量保存在闭包中:

function countCharacters( input, output, max ) {
    var $input = $(input);
    var $output = $(output);
    $output.text(max + ' characters left');
    $input
        .keydown(function(event) {
            if (event.keyCode != 8 &&
                event.keyCode != 46 &&
                $input.val().length >= max)
                event.preventDefault();
        })
        .keyup(function() {
            var val = $input.val().slice(0, max);
            var left = max - val.length;
            $input.val(val);
            $output.text(left + ' characters left');
        });
}
countCharacters('#description', '.character-count', 4000);
countCharacters('#another_description', '.character-count-2', 3000);

编辑:要专门处理相同的类,请使用兄弟选择器:

countCharacters('#description', '#description + div .character-count', 4000);
countCharacters('#another_description', '#another_description + div .character-count', 3000);

答案 1 :(得分:0)

只需在您的跨度中添加ID,例如

<li><textarea name="description" id="description"></textarea>
<div><span class="character-count" id="count1"></span></div></li>

<li><textarea name="another_description" id="another-description"></textarea>
<div><span class="character-count" id="count2"></span></div></li>

如果您只想影响描述的计数标签,请使用ID count1

 $('#count1').text(left + ' characters left');

答案 2 :(得分:0)

请尝试以下代码,您的代码非常好。我创建了一个函数,您可以在其中应用尽可能多的文本字段。请参考jsfiddle - https://jsfiddle.net/sashant9/8feprLfa/

HTML - &gt;

<span id="character-count-1" ></span>
<textarea id="description_1"></textarea>
</br></br>
<span id="character-count-2" ></span>
<textarea id="description_2"></textarea>

jQuery - &gt;

    var max = 4000;
    var textbox_selector_1 = '#description_1';
    var textbox_selector_2 = '#description_2';
    var count_indicator_1 = '#character-count-1';
    var count_indicator_2 = '#character-count-2';

    textbox_character_count(count_indicator_1, textbox_selector_1);
    textbox_character_count(count_indicator_2, textbox_selector_2);



function textbox_character_count(count_indicator_1, textbox_selector_1){
    $(count_indicator_1).text('4000 characters left');

       $(textbox_selector_1)
    .keydown(function(event){
        if (event.keyCode != 8 && event.keyCode != 46 && $(this).val().length >= max) {
            event.preventDefault();
        }
    })
    .keyup(function(){
        var $this = $(this),
            val = $this.val().slice(0, max),
            val_length = val.length,
            left = max - val_length;

        $(count_indicator_1).text(left + ' characters left');

        $this.val(val);
    });

}