我有下面的代碼,它將輸入限制爲只有數字和一定數量的組合鍵。但我也想限制輸入只接受一個小數點。敲除 - 將輸入限制爲只有一位小數的數字
number: {
init: function (element) {
// Allows only numbers with decimals
$(element).on("keydown", function (event) {
// Allow: backspace, delete, tab, escape, and enter
if (event.keyCode === 46 || event.keyCode === 8 || event.keyCode === 9 || event.keyCode === 27 || event.keyCode === 13 ||
// Allow: Ctrl+A, Ctrl + C, Ctrl + V, Ctrl + X
((event.keyCode === 65 || event.keyCode === 67 || event.keyCode === 86 || event.keyCode === 88) && (event.ctrlKey === true || event.metaKey === true)) ||
// Allow: .
(event.keyCode === 190 || event.keyCode === 110) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
} else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
}
}
我嘗試添加'$(element).val().indexOf('.') !== -1'
像下面,但它沒有做任何事情,我仍然可以儘可能多的時間我想鍵入英寸我應該如何限制它?
if (event.shiftKey || ($(element).val().indexOf('.') !== -1 && (event.keyCode === 190 || event.keyCode === 110)) || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
......然後我一起走而是將「lorem ipsum」粘貼到輸入中。 :-) –
@ T.J.Crowder沒想到那個哈哈.. – user3312508