[英]How can I use Javascript to tell whether an HTML input field has been changed by the browser?
onchange="myfunction()"
The above works perfectly when I want the Javascript function "myfunction" to execute as soon as a user inputs some text into an input field however, I have a situation where the browser automatically inputs the text. 当用户在输入字段中输入一些文本时,我希望Javascript函数“myfunction”能够执行,但是,我有一种浏览器自动输入文本的情况。 How can I execute myfunction when the field is updated with the following:
如何使用以下内容更新字段时如何执行myfunction:
document.getElementById("myfield").value = "my value"
"onchange" does not recognise DOM changes. “onchange”无法识别DOM更改。
onchange
only fires when the user types into the input and then the input loses focus. onchange
仅在用户键入输入时触发,然后输入失去焦点。
But you can trigger the event using: 但您可以使用以下命令触发事件:
$("#myfield").trigger("change");
$(function(){ $('#btn').click(function(){ document.getElementById('myField').value = "my value"; $('#myField').trigger('change'); }); }) function myfunction(){ alert('Value Changed'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type = "text" onChange = "myfunction()" id="myField"/> <button id="btn">Change </button>
onchange
only fires when the user types into the input and then the input loses focus. onchange
仅在用户键入输入时触发,然后输入失去焦点。
But you can trigger the event using: 但您可以使用以下命令触发事件:
$("#myfield").trigger("change");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.