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. How can I execute myfunction when the field is updated with the following:
当我希望Javascript函数“myfunction”能够在用户输入一些文本到输入字段时执行时,上面的工作是完美的,但是我有一个情况,浏览器会自动输入文本。当字段更新为以下内容时,如何执行myfunction:
document.getElementById("myfield").value = "my value"
"onchange" does not recognise DOM changes.
“onchange”不能识别DOM更改。
1 个解决方案
#1
3
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");
JSFIDDLE
#1
3
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");
JSFIDDLE