jQuery KeyFilter 插件问题
使用 jQuery 的 keyfilter 插件,除了一个问题之外,一切似乎都很好。
我正在使用正则表达式来过滤元素。
$('#nameVal').keyfilter(/[0-9a-zA-Z]/);
奇怪的是,这不仅允许输入字母数字字符,还允许输入“(”。事实上,无论我传递给密钥过滤器什么表达式,我都无法阻止“(”被允许在文本框中
有其他人遇到过这个问题吗?有解决方案吗?
Using keyfilter plugin for jQuery, and all seems to be fine apart from one problem.
I am using a regular expression to filter the element.
$('#nameVal').keyfilter(/[0-9a-zA-Z]/);
The odd thing is, this is not only allowing alpha-numeric characters but it is also allowing '(' to be entered. In fact, it doesn't matter what expression I pass to the keyfilter, I can't stop '(' being allowed in the textbox.
Has anyone else experienced this problem and is there a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题。
如果键码为 40(即“(”的键码),KeyFilter 插件中的 isSpecialKey 函数将返回 true。这意味着永远不会对字符执行测试。
注释掉上面的代码可以解决问题。不完全确定这在其他浏览器中会产生什么影响,但它在 IE8 和 Chrome 中有效。
I found the problem.
The isSpecialKey function in the KeyFilter plugin returns true if the keycode is 40, which is the keycode for '('. This means that the test is never performed on the character.
Commenting out the bit of code as I have above fixes the problem. Not entirely sure what effect this will have in other browsers, but it works in IE8 and Chrome.