用onkeydown
限制字符输入,oninput
与onpropertychange
监听输入框的值改变事件。演示地址:Ip-InputGit@OSC:Ip-Input
1、限制用户的键盘输入,只允许输入数字以及退格等操作,使用onkeydown
事件,监听键盘按键代码,如果不是我们允许的输入,则禁止输入。
1 2 3 4 5 6 7 8 9 10 11 |
$(document).on("keydown", ".ip", function(event) { /*禁止非法字符的输入*/ var k = event.which; if ((k >= 48 && k <= 57) || (k >= 96 && k <= 105) || k === 8 || k === 9 || k === 37 || k === 39 || k === 17 || k === 86) { if (this.value.length > 2) { /*限制输入长度*/ return k === 8 || k === 9 || k === 37 || k === 39; } } else { return false; } }) |