js監聽input輸入框值的即時變化執行個體,input輸入框

來源:互聯網
上載者:User

js監聽input輸入框值的即時變化執行個體,input輸入框

1、在元素上同時綁定 oninput 和onporpertychanger事件

例:

<script type="text/JavaScript">function aa(e){alert("inputting!!");}</script><input type="text" id="a" oninput="aa(event)" onporpertychange="aa(event)" />

2、使用原生js添加監聽事件

<script type="text/javascript"> $(function(){if("\v"=="v"){//true為IE瀏覽器,感興趣的同學可以去搜下,據說是現有最流行的判斷瀏覽器的方法document.getElementById("a").attachEvent("onporpertychange",function(e){console.log("inputting!!");}}else{document.getElementById("a").addEventListener("onporpertychange",function(e){console.log("inputting!!");}}});</script><input type="text" id="a"/>

3、使用jQuery方法綁定事件

<script type="text/javascript"> $(function(){$("#a").bind('input porpertychange',function(){console.log("e");});});</script><input type="text" id="a"/>

在監聽到 onpropertychange 事件後,可以使用 event 的 propertyName 屬性來擷取發生變化的屬性名稱,event.propertyName

執行個體1:

<input type="text" oninput=" " onpropertychange="" value="Text field" />

執行個體2:

$("#name").bind('input porpertychange',function(){    var thisTxt=$("#name").val();    $(this).siblings("p").html(thisTxt)  })

執行個體3:

//手機號碼分段顯示register.phonePropertychange = function() {  _this = register;  _input = $(this);  var v = $(this).val();  v = v.replace(new RegExp(/ /g),'');  var v1 = v.slice(0,3);  var v2 = v.slice(3,7);  var v3 = v.slice(7,11);  if(v2==''){    _input.focus().val(v1);  }else if(v3==''){    _input.focus().val(v1+' '+v2);  }else{    _input.focus().val(v1+' '+v2+ ' '+v3);  };   //手機號輸入完成字型顏色改變  if (v.length === 11) {    if(_this.regexpPhone(v)){      _input.css('color','#000');      $('#btnSendCode').addClass('c-26a949');      _input.blur();;    }else{      layer.open({content: '手機號碼不正確,請重新輸入',time: 2, end:function(){        _input.val('');      }});    }  }else{    _input.css('color','#26a949');  }}//驗證手機號register.regexpPhone = function(phone){  return /^1[3|4|5|7|8]\d{9}$/.test(phone);}

以上這篇js監聽input輸入框值的即時變化執行個體就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.