zoukankan      html  css  js  c++  java
  • jQuery/原生JS实时监听input输入框值变化

    input事件: 

    onchange

    1、要在 input 失去焦点的时候才会触发;

    2、在输入框内容变化的时候不会触发change,当鼠标在其他地方点一下才会触发;

    3、onchange event 所有主要浏览器都支持;

    4、onchange 属性可以使用于:<input>, <select>, 和 <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、在用户输入时触发,它是在元素值发生变化时立即触发;

    2、该事件在 <input> 或 <textarea> 元素的值发生改变时触发。

    3、缺陷:从脚本中修改值不会触发事件。从浏览器下拉提示框里选取值时不会触发。IE9 以下不支持,所以IE9以下可用onpropertychange 事件代替。

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

     

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

    onpropertychange:

    1、会实时触发,会在元素的属性改变时就触发事件。当元素disable=true时不会触发

    2、缺陷:只在IE 下支持,其他浏览器不支持,用oninput来解决。

     

    <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" />
    

      

     

  • 相关阅读:
    vue项目在IE中使用的一些坑(未完待续)
    Vue在IE下显示空白问题
    nginx 解决AJAX 跨域问题。
    node.js+express+jade 国际化
    Angular 2 HTTP Requests with Promise
    Windows x64编译 Qt5.7 Mysql驱动
    mysql [Err] 1215
    VS Code 调试Node.js express网站
    Node.js express 入门示例1
    build qt4.8.5 on centos7 or suse11.1
  • 原文地址:https://www.cnblogs.com/myprogramer/p/11691363.html
Copyright © 2011-2022 走看看