简体   繁体   English

使用单个函数更新多个div的charcount

[英]update charcount of multiple divs with single function

I am trying to use a code snippet I found online to implement charcount. 我正在尝试使用我在网上找到的代码片段来实现charcount。 It works for one single text area. 它适用于单个文本区域。 I have multiple text areas with different count limits. 我有多个具有不同计数限制的文本区域。 Here is the code that works for one single form. 这是适用于单个表单的代码。

Javascript:
function countChar(val,count,focus){

        var len = val.value.length;
    var lim=count;
    var focussed=focus;

    if (len >= lim) {

            $('#charNum').text(lim - len);
            $('#charNum').addClass('exceeded');
        /* val.value = val.value.substring(0, lim);*/   
        }else {
        if(focussed===0){
            $('#charNum').html('<a>&nbsp;</a>');
        }
                else {
            $('#charNum').text(lim - len);
            $('#charNum').removeClass('exceeded');
        }
        }
};

HTML:
<div id='charNum' class='counter'>&nbsp;</div>
<textarea id='description' name='description'  onkeyup=\"countChar(this,200,1)\" onblur=\"countChar(this,200,0)\" rows='10' cols='20'></textarea>

If I have two text areas, how can I modify this code to work ? 如果我有两个文本区域,如何修改此代码才能工作? I know how to get the id of the div in the script, But I dont know how to correctly update the counter div, say #charNum1, and #charNum2. 我知道如何在脚本中获取div的id,但我不知道如何正确更新counter div,比如#charNum1和#charNum2。 Appreciate some hints. 欣赏一些提示。 Thanks 谢谢

EDIT: I was thinking, I can name the counter div as "Charnum+divName" if that helps 编辑:我在想,如果有帮助,我可以将计数器div命名为“Charnum + divName”

If you attach your event handler(s) with jQuery you can use this within the handler to refer to whichever element the event was triggered on, thus avoiding having to hardcode element ids inside your function. 如果使用jQuery附加事件处理程序,则可以在处理程序中使用this来引用触发事件的任何元素,从而避免在函数内部硬编码元素ID。

I'd suggest adding an attribute with the max chars allowed and removing the inline event handlers: 我建议添加一个允许ma​​x chars的属性并删除内联事件处理程序:

<div id='charNum' class='counter'>&nbsp;</div>
<textarea id='description' name='description' data-maxChars="200" rows='10' cols='20'></textarea>
<div id='charNum2' class='counter'>&nbsp;</div>
<textarea id='otherfield' name='otherfield' data-maxChars="400" rows='10' cols='20'></textarea>

Then: 然后:

$(document).ready(function() {
    $("textarea[data-maxChars]").on("keyup blur", function(e) {
        var $this = $(this),
            $counter = $this.prev(),
            len = $this.val().length,
            maxChars = +$this.attr("data-maxChars");

        $counter.text(maxChars - len).toggleClass("exceeded", len > maxChars);
    });
});    

Demo: http://jsfiddle.net/nnnnnn/GTyW3/ 演示: http//jsfiddle.net/nnnnnn/GTyW3/

If each textarea is going to have it's own div, you can just add an extra parameter to the countChar function which would be the name of the div. 如果每个textarea都有自己的div,你可以在countChar函数中添加一个额外的参数,这个函数就是div的名字。 So you'd have something like: 所以你有类似的东西:

function countChar(val,count,focus, charCountDiv)

then, instead of hardcoding it in the function, the jQuery would be: 然后,jQuery不是在函数中硬编码,而是:

$(charCountDiv) 

That should do what I think you are looking to do. 这应该做我认为你想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM