Input限制輸入的數字為正數
input自己帶的限制type= number min=”0”(只在form表單內起作用)
通過js獲取input的值,然后去操作dom,當input的值小於0時,歸0
<input type="number" id="num" max="100" min="1" />
js
num.onchange = function(){
if(num.value < 0){
num.value = 0
}
}
input的pattern屬性,利用正則表達式,但是這個方法的兼容性不是很好,有興趣的自己去看(只在form表單內起作用)
利用keyup等事件,判斷value值,改變value的值
<input type="text"
onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" />
原文鏈接:https://blog.csdn.net/qq_34164814/article/details/81381506