我有一个文本框,应该只输入数字。它有一个js函数。
<asp:TextBox ID="txtPrintCount" runat="server" MaxLength="2" Style="width: 30px" onkeydown="return CheckNumeric(event);" />
function CheckNumeric(e) {
if (window.event) { // IE
if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
event.returnValue = false;
return false;
}
} else { // Firefox
if ((e.which < 48 || e.which > 57) & e.which != 8) {
e.preventDefault();
return false;
}
}
}
但是,数字键盘无法使用此功能。键盘上只有其他数字键起作用。
我是否忘记了什么?如何运行数字键?
最佳答案
Is there something I have forgotten for this? How can I run the numeric keys?
键码不同。数字从 48
到 57
。小键盘从 96
到 105
。
I've a textbox that should be entered just numeric.
但是您应该测试该值是否为整数,而不是测试键:
function isInteger(value) {
return value === parseInt(value, 10);
}
function CheckNumeric(e) {
if (window.event) { // IE
if (!isInteger(window.event.srcElement.value)) {
event.returnValue = false;
return false;
}
} else { // Firefox, Chrome, Edge, Safari, Opera, etc
if (!isInteger(e.target.value)) {
e.preventDefault();
return false;
}
}
}
关于javascript - 如何运行数字键来为asp.net文本框输入数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41116376/