在工作中,总是遇到很多禁止非数字字符输入的文本框,比如手机号码了 邮编了 复制代码 代码如下: <br>jQuery(document).ready(function($) { <br>// Stuff to do as soon as the DOM is ready; <br>var phone=$('#phone'); <br>$(phone).on('click',function(){ <br>phone.val(''); <br>}) <br>$(phone).on('keyup',function(evt){ <br>var phoneVal=phone.val(); <br>phoneVal=phoneVal.replace('/[^d] /g', ''); //替换非数字字符为空格 <br>phoneVal=parseInt(phoneVal,10); <br>if(isNaN(phoneVal)){ <br>phoneVal = ''; <br>} <br>this.value=phoneVal; <br>}) <br>}); <br>