我正在使用 javascript onkeyup 事件,我仅限制数字、退格键和选项卡按钮。我需要在我的代码中提及左右向上和向下箭头键...我如何在我的代码中提及...请帮助
这是我的代码
function Validatenumber(txt) {
txt.value = txt.value.replace(/[^0-9\n\r]+/g, '');
}
<input type="text" id="mobile" maxlength="11" name="Mobile" data-bind="value: Mobile" onkeyup = "Validatenumber(this)" />
最佳答案
以下代码用于检查输入的数字
JS 代码
function checkForOnlyNumbers(e){
var a = [];
var k = e.which;
a.push(0);
a.push(8);
a.push(127);
a.push(46);
for (i = 48; i < 58; i++)
a.push(i);
if (!(a.indexOf(k)>=0))
e.preventDefault();
};
并在 HTML 中添加按键监听器
<input type='text' onkeypress='checkForOnlyNumbers(event)' />
关于c# - Javascript onkeyup 事件(如何在我的代码中提及箭头键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342751/