An example of js listening to the real time change of input input box value

  • 2021-07-16 01:10:31
  • OfStack

1. Bind both oninput and onporpertychanger events on elements

Example:


<script type="text/JavaScript">
function aa(e){alert("inputting!!");}
</script>

<input type="text" id="a" oninput="aa(event)" onporpertychange="aa(event)" />

2. Add listening events using native js


<script type="text/javascript">
 $(function(){
if("\v"=="v"){//true For IE Browser, interested students can search, it is said to be the most popular way to judge the browser 
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. Bind events using the jQuery method


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

After listening for an onpropertychange event, you can use the propertyName property of event to get the changed property name, event. propertyName

Example 1:

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

Example 2:


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

Example 3:


// Segmented display of mobile phone number 
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);
  };
 
  // Mobile phone number input completes font color change 
  if (v.length === 11) {
    if(_this.regexpPhone(v)){
      _input.css('color','#000');
      $('#btnSendCode').addClass('c-26a949');
      _input.blur();;
    }else{
      layer.open({content: ' The mobile phone number is incorrect, please re-enter it ',time: 2, end:function(){
        _input.val('');
      }});
    }
  }else{
    _input.css('color','#26a949');
  }
}

// Verify the mobile phone number 
register.regexpPhone = function(phone){
  return /^1[3|4|5|7|8]\d{9}$/.test(phone);
}


Related articles: