简体   繁体   English

jqEasy计数器(jquery)-需要“返回”计数为2个字符-如何?

[英]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. 我正在使用jqEasy计数器对文本区域中的字符进行倒计时。 http://web.archive.org/web/20150317063551/http://www.jqeasy.com/jquery-character-counter 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 only counts it as one. 但是在我们的实现中,数据库将返回键计数为2个字符,而计数器仅将其计数为1个字符。 Basically the form submission takes the returns and makes them "/n" or something. 基本上,表单提交将返回值设为“ / n”或其他内容。

Does anyone have a recommendation on how I could modify this code to make the return key register 2 characters in the counter? 是否有人建议我如何修改此代码,以使返回键在计数器中注册2个字符?

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: 现在,以这种方式(第56行)计算插件的输入长度:

    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. 您应该做的是遍历val,找出返回键被按下了多少次,然后将该值累加到长度上。 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/ 这应该工作,看看小提琴http://jsfiddle.net/D27gs/2/

remember to attach this to the keyup event or it won't work 请记住将其附加到keyup事件上,否则将无法正常工作

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

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