Javascript 以编程方式更改输入值时如何触发 JQuery 更改事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33887499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fire JQuery change event when input value changed programmatically?
提问by Mohammad
I want to fire the JQuery change
event when the input text is changed programmatically, for example like this:
我想在以change
编程方式更改输入文本时触发 JQuery事件,例如:
$("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' />
But it doesn't work. How can I make this work?
但它不起作用。我怎样才能使这项工作?
回答by Pranav C Balan
change event only fires when the user types into the input and then loses focus.
仅当用户输入输入然后失去焦点时才会触发更改事件。
You need to trigger the event manually using change()
or trigger('change')
您需要使用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' />
回答by kawnah
The event handler .change()
behaves like a form submission - basically when the value changes on submit the console will log. In order to behave on text input you would want to use input, like below:
事件处理程序的.change()
行为类似于表单提交 - 基本上当提交时值更改时,控制台将记录。为了对文本输入进行操作,您需要使用输入,如下所示:
$("input").on('input', 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' />
回答by RageAgainstTheMachine
What you need to do is trigger the change
event after you've set the text. So you may create a function to do that so you won't have to repeat it every time you need to update the text, like this:
您需要做的是change
在设置文本后触发事件。因此,您可以创建一个函数来执行此操作,这样每次需要更新文本时就不必重复它,如下所示:
function changeTextProgrammatically(value) {
$("input").val( value );
$("input").trigger( 'change' ); // Triggers the change event
}
changeTextProgrammatically( "A" );
I've updated the fiddle,
我已经更新了小提琴,
回答by Dr Fred
You can use the DOMSubtreeModified event:
您可以使用 DOMSubtreeModified 事件:
$('input').bind('DOMSubtreeModified',function(){...})
If you want to fire bothuser and code changes:
如果你想火双方用户和代码更改:
$('input').bind('input DOMSubtreeModified',function(){...})
This event is marked as deprecated and sometimes quite CPU time consuming, but it may be also very efficient when used carefully...
此事件被标记为已弃用,有时会占用大量 CPU 时间,但如果谨慎使用,它也可能非常有效...
回答by Arun Kumar
jquery change event only works when the user types into the input and then loses focus. So you can use the following workaround to do so:- Let's say you have a button clicking on which results in change in value of input. (this could be anything else as well instead of a button)
jquery change 事件仅在用户输入输入然后失去焦点时才起作用。因此,您可以使用以下解决方法来执行此操作:- 假设您单击了一个按钮,该按钮会导致输入值发生变化。(这也可以是其他任何东西,而不是按钮)
var original_value = $('input').val();
$('button').click(function(){
var new_value = $('input').val();
if(original_value != new_value ){
//do something
}
//now set the original value to changed value (in case this is going to change again programatically)
original_value = new_value;
})