当 外部 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>

.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM