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

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

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

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


繁花如伊
浏览 92回答 1
1回答

拉风的咖菲猫

尝试下面的代码片段会起作用。<!DOCTYPE html><html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><script>$(document).ready(function(){&nbsp; $("input").change(function(){&nbsp; &nbsp; alert("The text has been changed.");&nbsp; &nbsp; $("#transferredValue").val(this.value)&nbsp; });});</script></head><body><input&nbsp; type="text"><p></p></body></html>Another Way you could try:<!DOCTYPE html>&nbsp; &nbsp; <html>&nbsp; &nbsp; <head>&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; $("#valueInput").on('change', function(){&nbsp; &nbsp; &nbsp; var changeValue =&nbsp; $("#valueInput").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(changeValue);&nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; <input type="text" id="valueInput" />&nbsp; &nbsp; <p></p>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>
打开App,查看更多内容
随时随地看视频慕课网APP