2016-05-06 17 views

回答

2

當您僅對數字使用<input type="number">時,則只允許使用javascript進行數字輸入。現在光標停留在中心:

input[type=number]{ 
 
    width: 100px; 
 
    text-align:center; 
 
}
<input type="number" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>

+1

謝謝。只想提及MDN說charCode已被棄用,並推薦使用KeyboardEvent.key。 – bewmar

0

如果您正在使用jQuery,還是願意使用,並且不希望被書面方式在所有數字輸入,行,你可以使用下面的代碼:

$(document.body).on('keypress',"input[type='number']", function(event){ 
    return event.charCode >= 48 && event.charCode <= 57; 
}) 

這將使同樣的效果,添加onkeypress事件屬性,修復您遇到的問題中心。