在工作中,總是遇到許多禁止非數字字元輸入的文字框,例如手機號碼了郵編了 複製代碼程式碼如下: <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>