如何在输入值以编程方式更改时触发JQuery更改事件? [英] How to fire JQuery change event when input value changed programmatically?

查看:94
本文介绍了如何在输入值以编程方式更改时触发JQuery更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编程改变输入文本时触发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事件。 p>

您需要使用 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'/>  


I want to fire the JQuery change event when the input text is changed programmatically, for example like this:

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

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

这篇关于如何在输入值以编程方式更改时触发JQuery更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆