Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • javascript 让你的文本框只输入数字

    post by onelong / 2009-10-10 20:22 Saturday [javascript]

    这是个例子:
    <html>
    <body>
    只能输入数字:<input id='txt' value="" maxlength="20" />
    <script language="javascript">
    function $(id){return document.getElementById(id)}
    function ie(){if(navigator.userAgent.indexOf("MSIE")>0){return true}else{return false}}
    function addevent(){
    if(ie()==true){
      $('txt').attachEvent("onpropertychange",txChange);
    }else{
      $('txt').addEventListener("input",txChange,false);
    }}
    function deleteevent(){
     if(ie()==true){
     $('txt').detachEvent("onpropertychange",txChange);}
     else{
     $('txt').removeEventListener("input",txChange,false)
     }
     }
    function txChange(){
     deleteevent();
     var txt=$('txt').value;
     txt=txt.replace(/\D+/g,'');
     $('txt').value=txt;
     addevent();
    }
    window.onload=addevent();
    </script>
    </body>
    </html>
         演示

    引用地址:
     

    我要评论