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>

    .

  • 相关阅读:
    POJ 1469 COURSES 二分图最大匹配
    POJ 1325 Machine Schedule 二分图最大匹配
    USACO Humble Numbers DP?
    SGU 194 Reactor Cooling 带容量上下限制的网络流
    POJ 3084 Panic Room 求最小割
    ZOJ 2587 Unique Attack 判断最小割是否唯一
    Poj 1815 Friendship 枚举+求最小割
    POJ 3308 Paratroopers 最小点权覆盖 求最小割
    1227. Rally Championship
    Etaoin Shrdlu
  • 原文地址:https://www.cnblogs.com/crazycode2/p/8081003.html
Copyright © 2011-2022 走看看