zoukankan      html  css  js  c++  java
  • 当 外部 input 值的改变,获取 当前 input type="hidden" 的值

    1.如何用jquery获取<input id="test" name="test" type="text"/>中输入的值?

    方法一:

    $("#test").val()

    方法二:

    $("input[name='test']").val()

    方法三:

    $("input[type='text']").val()

    方法四:

    $("input[type='text']").attr("value")

    2.当 input type="hidden" 的值发生改变时,触发自定事件,获取改变后的值

    demo_1.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <script src="jquery-1.8.2.js"></script>
    </head>
    <body>
      <input type="text" name="current" />
      <input type="hidden" name="hidden" value="0">
    
      <script>
        $("input[type='text']").on('input',function(){
          var currentVal = $("input[type='text']").val();
          // 触发 改变 input type="hidden" 的值
          $("input[type='hidden']").trigger('input:changed', currentVal);
        })
        // 给 input type="hidden" 添加自定义事件
        $("input[type='hidden']").on('input:changed', function (e, val) {
          // 获取 input type="hidden" 的值
            console.log(val);
        });
      </script>
    </body>
    </html>
    

    demo_2.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <script src="jquery-1.8.2.js"></script>
    </head>
    <body>
      <input type="text" name="current" />
      <input type="hidden" name="hidden" onpropertychange="changeVal()" value="0">
    
      <script>
        $("input[type='text']").on('input',function(){
          // 触发绑定方法
          changeVal();
        })
        // 给 input type="hidden" 添加 onpropertychange事件
        function changeVal(){
          // 获取当前值
          console.log($("input[type='hidden']").val()); // 0
        }
      </script>
    </body>
    </html>

    .

  • 相关阅读:
    套题 8.22
    套题 8.21
    P1042 乒乓球
    套题8.20
    #52. 【UR #4】元旦激光炮 (交互式题)
    #82. 【UR #7】水题生成器
    度度熊与邪恶大魔王
    wpf 获取image控件的图片并保存
    wpf 让正执行的程序暂停几秒钟
    wpf问题集锦
  • 原文地址:https://www.cnblogs.com/crazycode2/p/8081003.html
Copyright © 2011-2022 走看看