在项目中,有时候要只能输入数字的输入框,今日整理一下

禁止右键和粘贴

<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false"  onpaste="return false" >

只能输入数字

 onKeypress="return /[\d]/.test(String.fromCharCode(event.keyCode)))"

input框的设置

<input name="number" autocomplete="off" class="layui-input" placeholder="购买时长"
style="ime-mode:disabled" onfocus=" this.style.imeMode='disabled' "
onkeypress = 'return /^\d$/.test(String.fromCharCode(event.keyCode||event.keycode||event.which))'
oninput= 'this.value = this.value.replace(/\D+/g, "")'
onpropertychange='if(!/\D+/.test(this.value)){return;};this.value=this.value.replace(/\D+/g, "")'
onblur = 'this.value = this.value.replace(/\D+/g, "")'/>

这样就完美的解决了

 

注意不要用 type=number,  在h5中新加入的这个属性有一个bug,就是如果输入框中有非数字,无论是用js原生的还是jquery都获取不到输入框中的值

posted on 2017-06-29 11:20  坚持不懈❥  阅读(262)  评论(0编辑  收藏  举报