gpt4 book ai didi

javascript - jquery keypress() 事件不适用于 keyup()

转载 作者:行者123 更新时间:2023-11-30 12:16:52 24 4
gpt4 key购买 nike

这是按键功能的代码,它只允许数字 http://jsfiddle.net/lesson8/HkEuf/1/

但是,对于相同的键码,keyup 函数不起作用。我的意思是,如果我使用

$(document).ready(function () {
//called when key is pressed in textbox
$("#quantity").keyup(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});

使用 keyup 的原因是获取当前在文本框中输入的值。如果我使用 keyup 函数,我将获得当前值。但是,如果我使用 keydown 或 keypress,我将在文本框中获取先前的或现有的值查看具有不同功能的更新代码 http://jsfiddle.net/dgireeshraju/HkEuf/7300/这是使用 keydown 的示例,它给出了现有值。

最佳答案

KeyUp 仅在插入字符后触发,如您所见,您的函数实际上正在调用并且正在显示警告消息。

如果您使用 KeyDown 尝试相同的代码,它将起作用,因为在插入字符之前将调用该事件

//called when key is pressed in textbox
$("#quantity").keydown(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});

关于javascript - jquery keypress() 事件不适用于 keyup(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32218348/

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