如何给每个input都加上一个限制,只能输入数字与小数点?

https://img3.mukewang.com/5c89b80e00010bdd08000212.jpg

input的type=number之后,出现上下加减的标志
https://img1.mukewang.com/5c89b810000199cc01650084.jpg


慕桂英4014372
浏览 2567回答 3
3回答

qq_花开花谢_0

input type="number"

一只萌萌小番薯

复制的一个二次开发软件里的写法input中添加属性 onkeypress="return WST.isNumberdoteKey(event)" onblur="javascript:WST.limitDecimal(this,2)" onkeyup="javascript:WST.isChinese(this,1)"三个js函数可以添加到公共js文件中//只能輸入數字和小數點WST.isNumberdoteKey = function(evt){&nbsp;var e = evt || window.event;&nbsp;&nbsp;var srcElement = e.srcElement || e.target;&nbsp;&nbsp;var charCode = (evt.which) ? evt.which : event.keyCode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;if (charCode > 31 && ((charCode < 48 || charCode > 57) && charCode!=46)){&nbsp; &nbsp; &nbsp;return false;&nbsp;}else{&nbsp; &nbsp; &nbsp;if(charCode==46){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var s = srcElement.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(s.length==0 || s.indexOf(".")!=-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;return true;&nbsp;}}WST.limitDecimal = function(obj,len){var s = obj.value;&nbsp;if(s.indexOf(".")>-1){&nbsp; &nbsp; &nbsp;if((s.length - s.indexOf(".")-1)>len){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;obj.value = s.substring(0,s.indexOf(".")+len+1);&nbsp; &nbsp; &nbsp;}}&nbsp;s = null;}WST.isChinese = function(obj,isReplace){&nbsp;var pattern = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/i&nbsp;if(pattern.test(obj.value)){&nbsp; &nbsp; &nbsp;if(isReplace)obj.value=obj.value.replace(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/ig,"");&nbsp; &nbsp; &nbsp;return true;&nbsp;}&nbsp;return false;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript