在文本框中仅接受数字-jQuery [英] Accept only numbers in Textbox - jQuery
本文介绍了在文本框中仅接受数字-jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为文本框实现仅允许数字的功能.我已经编写了以下代码,但是使用Ctrl + V,我们可以粘贴文本.我们如何防止这种情况.
I need to implement a functionality for a textbox to allow only numbers. I have written the following code, but using Ctrl + V we are able to paste text. How can we prevent this.
$('.numeric-textbox').live('keypress', function (e) {
if ((e.keyCode < 48) || (e.keyCode > 57)) {
return false;
}
});
有人可以提出一些解决方案吗? :)
Can someone suggest some solution. :)
推荐答案
尝试
$('.numeric-textbox').bind("cut copy paste",function(e) {
e.preventDefault();
});
这将防止在文本框中粘贴剪切副本事件
This will prevent cut copy paste event on your textbox
这篇关于在文本框中仅接受数字-jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文