jQuery / JS native real-time monitoring input value changes in the input box

input events: 

onchange

1, the input to lose focus will trigger a time;

2, when the input box content changes will not trigger change, when the mouse elsewhere tap will trigger;

3, onchange event in all major browsers support;

4, onchange property can be used to: <input>, <select>, and <textarea>.

<script>
    function change(){
        var x=document.getElementById("password");
        x.value=x.value.toUpperCase();
    console.log("出发了") } </script> </head> <body> 输入你的密码: <input type="text" id="password" onchange="change()"> </body>

oninput:

1, when the user inputs a trigger, which is triggered immediately when the element value is changed;

2, the event is triggered when the value <input> or <textarea> element is changed.

3, defect: from the script does not change the value of the triggering event. When prompted not trigger drop-down box to select a value from the browser. IE9 does not support the following, so IE9 available onpropertychange the following events instead.

JS: <input type="text" id="password" oninput="change()">

 

jQuery: $("#password").on('input propertychange', change);

onpropertychange:

1, it will trigger real-time, event will be triggered when the properties change element. When the element does not trigger when the disable = true

2, defect: only in IE support, other browsers do not support using oninput to resolve.

 

<input type="text" id="password" oninput="onpropertychange()">

 jQuery:

<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>RunJS</title>  
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>  
    <body>  
        <input type="text" id="password" autoComplete='off'>  
      <script type="text/javascript">
$(function(){  
  $('#password').bind('input propertychange', function() {  
     console.log('在实时触发!!!')   $('#result').html($(this).val().length);
     $(this).val().length != 0 ? $("#login").css("background-color", "#086AC1") : $("#login").css("background-color", "#529DE0")   }); }) </script> </body> </html>

 JavaScript;

  <script type="text/javascript">
    // Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9
        function OnInput (event) {
            alert ("The new content: " + event.target.value);
        }
    // Internet Explorer
        function OnPropChanged (event) {
            if (event.propertyName.toLowerCase () == "value") {
                alert ("The new content: " + event.srcElement.value);
            }
        }
 </script>
 
 <input type="text" oninput="OnInput (event)" onpropertychange="OnPropChanged (event)" value="Text field" />

  

 

Guess you like

Origin www.cnblogs.com/myprogramer/p/11691363.html