我是jQuery的新手,對於如何在輸入框中只獲取1到5的數字有點困惑。以下是我的代碼:只想在輸入框中使用jQuery輸入數字1到5
HTML
<div id="errmsg"></div>
<input type="text" id="number" maxlength="1"/>
SCRIPT
$(document).ready(function(e) {
$('#number').keydown(function(e){
if (e.which != 8 && e.which != 0 && (e.which < 49 || e.which > 53)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
else if (e.which == 48 || e.which < 48 || e.which > 53) {
//display error message
$("#errmsg").html("5 Only").show().fadeOut("slow");
return false;
}
})
})
</script>
當用戶輸入在輸入框中的任何量,如果他輸入任何字符然後錯誤消息僅顯示「Digits」。如果他輸入的數字超過5,則會顯示錯誤「僅5」消息。
可能的重複[如何只允許數字(0-9)在HTML輸入框使用jQuery?](http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9 -in-html-inputbox-using-jquery) – Andreas
在這種情況下,對於用戶來說,0..5的'select'會更好/更清晰嗎? – Raidri
感謝您的建議,但我只需要使用輸入 – Kamal