Home > Web Front-end > JS Tutorial > body text

Example of js monitoring real-time changes in input input box value

高洛峰
Release: 2017-02-03 14:05:08
Original
2203 people have browsed it

1. Bind oninput and onporpertychanger events to the element at the same time

Example:

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

2. Use native js to add listening events

<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"/>
Copy after login

3. Use the jQuery method to bind events

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

After listening to onpropertychange After the event, you can use the event's propertyName attribute to get the changed property name, event.propertyName

Instance 1:

Example 2:

$("#name").bind(&#39;input porpertychange&#39;,function(){
    var thisTxt=$("#name").val();
    $(this).siblings("p").html(thisTxt)
  })
Copy after login

Example 3:

//手机号码分段显示
register.phonePropertychange = function() {
  _this = register;
  _input = $(this);
  var v = $(this).val();
  v = v.replace(new RegExp(/ /g),&#39;&#39;);
  var v1 = v.slice(0,3);
  var v2 = v.slice(3,7);
  var v3 = v.slice(7,11);
  if(v2==&#39;&#39;){
    _input.focus().val(v1);
  }else if(v3==&#39;&#39;){
    _input.focus().val(v1+&#39; &#39;+v2);
  }else{
    _input.focus().val(v1+&#39; &#39;+v2+ &#39; &#39;+v3);
  };
  
  //手机号输入完成字体颜色改变
  if (v.length === 11) {
    if(_this.regexpPhone(v)){
      _input.css(&#39;color&#39;,&#39;#000&#39;);
      $(&#39;#btnSendCode&#39;).addClass(&#39;c-26a949&#39;);
      _input.blur();;
    }else{
      layer.open({content: &#39;手机号码不正确,请重新输入&#39;,time: 2, end:function(){
        _input.val(&#39;&#39;);
      }});
    }
  }else{
    _input.css(&#39;color&#39;,&#39;#26a949&#39;);
  }
}
 
//验证手机号
register.regexpPhone = function(phone){
  return /^1[3|4|5|7|8]\d{9}$/.test(phone);
}
Copy after login

The above example of js monitoring the real-time change of the input input box value is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more js monitoring real-time changes in the input input box value and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!