久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      jquery怎樣設(shè)置只能填數(shù)字

      jquery中,可用replace()方法和正則表達(dá)式來(lái)設(shè)置只能填寫(xiě)數(shù)字,replace()方法用于替換一個(gè)與正則表達(dá)式匹配的子串,當(dāng)填寫(xiě)內(nèi)容不是數(shù)字時(shí)就不顯示,語(yǔ)法為“指定元素對(duì)象.val().replace(/[^d]/g,"")”。

      jquery怎樣設(shè)置只能填數(shù)字

      本教程操作環(huán)境:windows10系統(tǒng)、jquery3.2.1版本、Dell G3電腦。

      jquery怎樣設(shè)置只能填數(shù)字

      有的input文本框需要做一些判斷,只能輸入數(shù)字,這個(gè)判斷可以通過(guò)正則表達(dá)式進(jìn)行判斷,當(dāng)用戶輸入的內(nèi)容不是數(shù)字就不在input文本框顯示,讓我們來(lái)看一下jq如何設(shè)置只能輸入數(shù)字。

      語(yǔ)法:

      $('input').on('input propertychange', function(e) { var text = $(this).val().replace(/[^d]/g, ""); $(this).val(text)    })

      新建html文件。

      在html文件上創(chuàng)建一個(gè)input輸入框,然后設(shè)置輸入框的大小。

      代碼:<input type="text" style="width: 190px;">

      引入jquery.min.js文件。

      代碼:<script type="text/javascript" src="jquery-1.9.1.min.js"></script>

      用正則表達(dá)式判斷用戶每次輸入的內(nèi)容,當(dāng)內(nèi)容不是數(shù)字就不顯示。

      代碼:

      <script> $(function(){ $('input').on('input propertychange', function(e) { var text = $(this).val().replace(/[^d]/g, ""); $(this).val(text)    }) }) </script>

      如圖:

      jquery怎樣設(shè)置只能填數(shù)字

      保存后html文件后,適用瀏覽器打開(kāi)進(jìn)行測(cè)試,發(fā)現(xiàn)輸入非數(shù)字的內(nèi)容無(wú)法在文本框上顯示。如圖:

      jquery怎樣設(shè)置只能填數(shù)字

      只需要把所有代碼復(fù)制到新建的html文件上粘貼,保存后即可看到效果。

      所有代碼:

      <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/jquery-3.3.1.min.js" ></script> <script> $(function(){ $('input').on('input propertychange', function(e) { var text = $(this).val().replace(/[^d]/g, ""); $(this).val(text)    }) }) </script> </head> <body> <input type="text" style="width: 190px;"> </body> </html>

      相關(guān)視頻教程推薦:jQuery視頻教程

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)