我想在以编程方式更改输入文本时触发 JQuery change
事件,例如:
$("input").change(function(){
console.log("Input text changed!");
});
$("input").val("A");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' />
但它不起作用。我怎样才能使这项工作?
最佳答案
change event only fires when the user types into the input and then loses focus.
您需要使用 change()
手动触发事件 或 trigger('change')
$("input").change(function() {
console.log("Input text changed!");
});
$("input").val("A").change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' />
关于javascript - 当输入值以编程方式更改时如何触发 JQuery 更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887499/