KeyCode 在 mozilla 中出错
KeyCode is taking wrong in mozilla
我的 javascript 代码禁用文本框 ix 中的小数,如下所示
$(document).ready(function() {
$(".money-input").keypress(function (e) {
// between 0 and 9
if (e.which < 48 || e.which > 57) {
alert(1);
showAdvice(this, "Integer values only");
return (false); // stop processing
}
});
function showAdvice(obj, msg) {
$("#singleAdvice").stop(true, false).remove(); // remove any prev msg
$('<span id="singleAdvice" class="advice">' + msg + '</span>').insertAfter(obj);
$("#singleAdvice").delay(4000).fadeOut(1500); // show for 4 seconds, then fade out
}
});
这在 Chrome 中非常有效。但是当我去 Mozilla 时。返回 space alos 进入那个 if case。因此,当我按回 space 时,什么也没有发生,并显示错误消息。谁能在这里指出我做错了什么?
keypress
事件不会针对不可打印的字符触发,例如 Esc、Ctrl、Shift,在你的情况下,退格。
改用keyup()
:
$(".money-input").keyup(function(e) {
if (e.which < 48 || e.which > 57) {
showAdvice(this, "Integer values only");
return false;
}
});
我的 javascript 代码禁用文本框 ix 中的小数,如下所示
$(document).ready(function() {
$(".money-input").keypress(function (e) {
// between 0 and 9
if (e.which < 48 || e.which > 57) {
alert(1);
showAdvice(this, "Integer values only");
return (false); // stop processing
}
});
function showAdvice(obj, msg) {
$("#singleAdvice").stop(true, false).remove(); // remove any prev msg
$('<span id="singleAdvice" class="advice">' + msg + '</span>').insertAfter(obj);
$("#singleAdvice").delay(4000).fadeOut(1500); // show for 4 seconds, then fade out
}
});
这在 Chrome 中非常有效。但是当我去 Mozilla 时。返回 space alos 进入那个 if case。因此,当我按回 space 时,什么也没有发生,并显示错误消息。谁能在这里指出我做错了什么?
keypress
事件不会针对不可打印的字符触发,例如 Esc、Ctrl、Shift,在你的情况下,退格。
改用keyup()
:
$(".money-input").keyup(function(e) {
if (e.which < 48 || e.which > 57) {
showAdvice(this, "Integer values only");
return false;
}
});