Switching between two jQuery counters
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?
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.
精彩评论