Javascript jquery keypress()事件对keyup()不起作用
这是按键功能的代码,它只允许数字 但是,对于相同的keycodes,keyup功能不起作用。我是说,如果我使用Javascript jquery keypress()事件对keyup()不起作用,javascript,jquery,Javascript,Jquery,这是按键功能的代码,它只允许数字 但是,对于相同的keycodes,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
$(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;
}
});
});
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“#数量”).keyup(函数(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});
使用keyup的原因是为了在文本框中获取当前输入的值。如果我使用keyup
函数,我将获得当前值。但是,如果我使用keydown或keypress,我会在文本框中获取以前或现有的值
请参阅具有不同功能的更新代码
这是keydown的例子,它给出了现有的值
Key up在执行该Key>的默认操作后,当用户释放某个Key时激发
当在文本输入中插入实际字符(例如>时)时,按键触发。当用户按下按键时,它会重复
您的代码在这两种情况下都能正常工作(您至少可以看到错误消息)
但由于此事件不同,因此结果也不同。要使其与keyup
一起工作,您需要再次清空输入元素,因为此时该值已经输入到输入元素中
$("#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
$(this).val(''); //<--- this will empty the value in the input.
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
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;
}
});
//在文本框中按下键时调用
$(“#数量”).keydown(函数(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
为什么要使用keyup而不是keypress->use keydown而不是use.keydown(),如果仍然要使用keypress,我想您必须在返回false之前清除该值$(this).val(“”)将删除完整值,即使该值有一些数字,我也可以使用.keydown
,但控制台.log(e.currentTarget.value)代码>给出了上一个数字。我的意思是,如果文本框已经有2,我输入3,它不会显示23。但在console.log中显示2。我想在文本框中的console.log中获取当前值。希望你能理解@prashant一点小技巧应该有用:)。。我其实不喜欢这个,但你可以;)::pI可以使用.keydown,但不能使用console.log(e.currentTarget.value);正在给出以前的号码。我的意思是,如果文本框已经有2,我输入3,它不会显示23。但在console.log中显示2。我想在文本框中的console.log中获取当前值。希望您能理解是的,这是因为当您单击3并尝试在向下键中登录时,它无法记录“3”,因为它尚未插入。
//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;
}
});