在两个 jQuery 计数器之间切换

发布于 2024-10-16 01:51:16 字数 869 浏览 0 评论 0原文

用户可以在两种消息类型之间进行选择,每种消息类型都有不同的长度。支持 160 个字符和 1071 个字符的消息类型。

通常用户会选择一种类型并编写他的消息。但在他写作时,他可能会更改消息类型。

这就是我使用计数器的方式:

switch(max){
        case 160:
            $('#message').NobleCount('#messageInfo',{
                max_chars: max,
                block_negative: true
            });
        break;
        case 1071:  
            $('#message').NobleCount('#messageInfo',{
                max_chars: max,
                block_negative: true,
                on_update: function(t_obj, char_area, c_settings, char_rem){
                    //...
                }
            });     
        break;
    }

问题是当选择 160 并且用户已写入 160 个字符并切换到 1071 个字符时,计数器显示剩余 911,但不允许用户输入更多字符。

每当用户切换消息类型时,我都会调用上面的代码。有人对这个问题有想法吗?也许noble-count应该以某种方式删除?但就我而言,它不是被覆盖了吗?

the user can choose between two message types, each with different length. 160 chars and 1071 chars are supported message types.

Well normally the user would select a type and write his message. But while he is writing he may change the message type.

This is how I use the counter:

switch(max){
        case 160:
            $('#message').NobleCount('#messageInfo',{
                max_chars: max,
                block_negative: true
            });
        break;
        case 1071:  
            $('#message').NobleCount('#messageInfo',{
                max_chars: max,
                block_negative: true,
                on_update: function(t_obj, char_area, c_settings, char_rem){
                    //...
                }
            });     
        break;
    }

The problem is when 160 is selected and the user has written 160 chars and switches to 1071 chars the counter shows 911 as remaining but does not allow the user to input further chars.

I call the code above everytime the user switches message types. Do anyone has an idea on that problem? Maybe noble-count should just be removed somehow? But isn't it overwritten anyway in my case?

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

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

发布评论

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

评论(1

失眠症患者 2024-10-23 01:51:16

原因是 NobleCount 不会重置它绑定的事件侦听器。因此,当您第二次调用 .NobleCount() 时,尽管它添加了一个事件侦听器来阻止您输入较大数量的 max_chars,但旧事件侦听器仍然存在,因此它将继续阻止您输入较小数量的 max_chars。

解决方案是向 NobleCount 文件添加 2 个函数调用:

在第 285 行和第 294 行,更改:

-- $(t_obj).keydown(function(e) {
++ $(t_obj).unbind('keydown').keydown(function(e) {

-- $(t_obj).keyup(function(e) {
++ $(t_obj).unbind('keyup').keydown(function(e) {

(-- 是旧行,++ 是要替换的行)

有关工作示例,请参阅: http://jsfiddle.net/Fm5dC/
(我实际上将他的整个js文件从github复制到jsfiddle中,所以它看起来很难看,尽管如果你在javascript框架中滚动,你会看到我实现了上述更改)

我可能应该提到这不是最好的解决方法,因为如果您还有其他一些 keydownkeyup 监听器,它也会删除它们,尽管我已经向您展示了问题所在,但从这里开始使此修复更加可靠并不难。

另外,请联系插件的作者并提供错误描述、解决方法等,以便他可以为大众实现这一点。

The reason is that NobleCount doesnt reset the event listeners that it binds. So when you call .NobleCount() the second time, although it adds an event listener that will stop you entering the larger amount of max_chars, the old one is still there, so it will continue blocking you from entering the smaller amount of max_chars.

The solution is to add 2 function calls to the NobleCount file:

On lines 285 and 294, change:

-- $(t_obj).keydown(function(e) {
++ $(t_obj).unbind('keydown').keydown(function(e) {

-- $(t_obj).keyup(function(e) {
++ $(t_obj).unbind('keyup').keydown(function(e) {

(-- is the old line, ++ is the line to replace it with)

For a working example see: http://jsfiddle.net/Fm5dC/
(I literally copied his entire js file from github into jsfiddle, so it looks ugly, although if you scroll in the javascript frame you'll see that ive implemented the aforementioned change)

I should probably mention that this is not the best workaround because if you have some other keydown or keyup listener it will remove them as well, although ive showed you where the problem is, it's not that hard to make this fix more solid from here.

Also, please contact the author of the plugin with the bug description, this workaround etc. so he can implement this for the masses.

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