开发者

jqEasy Counter (jquery) - need to have "return" count as 2 characters - how?

I'm using the jqEasy counter to countdown characters in a text area. http://web.archive.org/web/20150317063551/http://www.jqeasy.com/jquery-character-counter

In our implementation though--the database counts a return key as 2 characters while the counter o开发者_StackOverflow中文版nly counts it as one. Basically the form submission takes the returns and makes them "/n" or something.

Does anyone have a recommendation on how I could modify this code to make the return key register 2 characters in the counter?

Thanks!


No you can't unless you change the code of the plugin. Right now the length of the input is calculated in this way (line 56) of the plugin:

    var val = $this.val(),
    length = val.length

Change it this way:

    var val = $this.val(),
    length = val.length
    //returns is an array: if i have two newline characters, it has three elemnts, 3 newline, four elements
    var returns = val.split('\n');
    length += returns.length -1;

What you sholud do is iterate over val and find out how many times the return key has been pressed and then sum that value to the lenght. Unfortunately i don't know how to find out "returns" in a string

EDIT - This is what you can do:

<textarea id='countIt'></textarea>

$('#countIt').keyup(function(){
    var value = $('#countIt').val();
    var returns = value.split('\n');
    var total = value.length;
    total += returns.length -1;
    console.log(total);//change with alert if no firebug
});

This should work, look at the fiddle http://jsfiddle.net/D27gs/2/

remember to attach this to the keyup event or it won't work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜