jqEasy Counter (jquery) - 需要有“返回”算作 2 个字符 - 怎么算?

发布于 2024-11-15 18:03:41 字数 379 浏览 3 评论 0原文

我正在使用 jqEasy 计数器对文本区域中的字符进行倒计时。 http://web.archive.org /web/20150317063551/http://www.jqeasy.com/jquery-character-counter

但在我们的实现中——数据库计算返回键计为 2 个字符,而计数器仅将其计为 1 个。基本上,表单提交会获取返回值并将其设置为“/n”或其他内容。

有人建议我如何修改此代码以使返回键在计数器中注册 2 个字符吗?

谢谢!

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 only 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2024-11-22 18:03:41

不,除非您更改插件的代码,否则您不能。现在输入的长度是按照插件的这种方式(第 56 行)计算的:

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

改变它:

    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;

你应该做的是迭代 val 并找出按回车键的次数,然后对该值求和到了长度。不幸的是,我不知道如何找出字符串中的“返回”

编辑 - 这就是你可以做的:

<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
});

这应该可行,看看小提琴 http://jsfiddle.net/D27gs/2/

记住将其附加到 keyup 事件,否则它将不起作用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文