简体   繁体   English

以编程方式更改输入值时如何触发 JQuery 更改事件?

[英]How to fire JQuery change event when input value changed programmatically?

I want to fire the JQuery change event when the input text is changed programmatically, for example like this:我想在以编程方式更改输入文本时触发 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' />

But it doesn't work.但它不起作用。 How can I make this work?我怎样才能使这项工作?

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' />

The event handler .change() behaves like a form submission - basically when the value changes on submit the console will log.事件处理程序.change()行为类似于表单提交 - 基本上当提交时的值更改时,控制台将记录。 In order to behave on text input you would want to use input, like below:为了对文本输入进行操作,您需要使用输入,如下所示:

 $("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' />

What you need to do is trigger the change event after you've set the text.您需要做的是在设置文本后触发change事件。 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:因此,您可以创建一个函数来执行此操作,这样每次需要更新文本时就不必重复它,如下所示:

function changeTextProgrammatically(value) {
    $("input").val( value );
    $("input").trigger( 'change' ); // Triggers the change event
}

changeTextProgrammatically( "A" );

I've updated the fiddle ,我已经更新了小提琴

You can use the DOMSubtreeModified event:您可以使用 DOMSubtreeModified 事件:

$('input').bind('DOMSubtreeModified',function(){...})

If you want to fire both user 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 时间,但如果小心使用,它也可能非常有效...

jquery change event only works when the user types into the input and then loses focus. jquery change 事件仅在用户输入输入然后失去焦点时才起作用。 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) (这也可以是其他任何东西,而不是按钮)

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;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在 jQuery 中更改了多个输入元素的值,则触发更改事件 - fire on change event if value of multiple input elements are changed in jQuery 以编程方式更新输入值时发生火灾更改事件 - Fire change event when programmatically update input value 当输入值以编程方式更改时触发更改事件? - Trigger Change event when the Input value changed programmatically? 使用JavaScript更改值时如何触发TextBox更改事件? - How to fire TextBox change event when value is changed using javascript? jQuery / Javascript-当按钮的值更改时,如何触发事件? - jQuery/Javascript - How to fire an event when a button's value is changed? 如何为javascript更改输入值触发事件 - how to fire event for javascript changed input value 以编程方式触发输入的更改事件 - Fire on change event of input programmatically 当输入值从 VueJS 更改时,JQuery 更改事件未触发 - JQuery change event not firing when input value changed from VueJS 选择下拉列表时要触发的JQuery事件 - 但值不会更改 - JQuery event to fire when a drop down is selected — but the value is not changed 当特定选项的选择值未更改或值更改为特定选项时,如何使用Jquery触发事件 - How to Fire an Event with Jquery when a select value is unchanged for a specific option or value is changed to a specific option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM