JQuery动态更改值取决于输入值



我正在尝试对输入进行更改侦听器,它必须将值设置为trans。字段,当用户键入要输入的内容时。

    $("#valueInput").on("change", function () {
        $("#transferredValue").val(this.value)
    })

但它没有起作用,什么也没发生。脚本已加载,我通过控制台进行了检查。

试试下面的代码片段就可以了。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("input").change(function(){
    alert("The text has been changed.");
    $("#transferredValue").val(this.value)
  });
});
</script>
</head>
<body>
<input  type="text">
<p></p>
</body>
</html>

Another Way you could try:

<!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    $("#valueInput").on('change', function(){
      var changeValue =  $("#valueInput").val();
         alert(changeValue);
       });
    });
    </script>
    </head>
    <body>

    <input type="text" id="valueInput" />
    <p></p>
    </body>
    </html>

最新更新