使用html代碼解決
<input type="number" maxlength="5" oninput="if(value.length>5)value=value.slice(0,5)" />
使用js代碼解決
//輸入框爲數字類型時防止maxlength屬性不起作用
$('input').bind('input propertychange', function() {
if($(this).attr("type")=="number"){
//獲取輸入框的最大長度
var mxaL= $(this).attr("maxlength");
//如果輸入的長度超過最大長度
if($(this).val().length>mxaL){
$(this).val($(this).val().slice(0,mxaL));
}
}
});