js监听input输入框值的实时变化实例

一、在元素上同时绑定 oninput 和onporpertychanger事件javascript

例:css

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

二、使用原生js添加监听事件html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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" />

三、使用jQuery方法绑定事件java

?
1
2
3
4
5
6
7
8
<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" />this

实例2:spa

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

实例3:.net

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//手机号码分段显示
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);
}

转载自:http://www.jb51.net/article/104323.htmcode

相关文章
相关标签/搜索