gpt4 book ai didi

jquery - 验证文本框 jQuery 上按键上的用户输入

转载 作者:行者123 更新时间:2023-12-01 00:46:34 25 4
gpt4 key购买 nike

如何在 jQuery 中进行编码,以便用户无法按点“.”在文本框中键入?

我可以像这样用javascript编写它,在每次按键时,浏览器都会检查是否按下了某个键,如果是点,则为子字符串(0,len-1),但它会闪烁!我想完全阻止按键!

最佳答案

这应该有效。它尚未经过跨浏览器测试:

$("#theTextBox").keyup(function() {
if($(this).val().indexOf('.') !== -1) {
var newVal = $(this).val().replace('.', '');
$(this).val(newVal);
}
});

You can try it here.

编辑:我认为这样更好:

function doItPlease() {
if ($(this).val().indexOf('.') !== -1) {
var newVal = $(this).val().replace('.', '');
$(this).val(newVal);
}
}

$("#theTextBox").bind("keydown keyup", doItPlease);

Try the less sucky solution here.

编辑(再次):我赞成上述解决方案,因为我非常喜欢反馈方面。也就是说,我认为这就是您所追求的:

$("#theTextBox").keyup(function(e) {
if (e.which != 190) {
return true;
}
e.preventDefault();
});

Try it here.

关于jquery - 验证文本框 jQuery 上按键上的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5509194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com