Home  >  Article  >  Web Front-end  >  jquery: solution to the problem that the change event does not trigger

jquery: solution to the problem that the change event does not trigger

黄舟
黄舟Original
2018-05-18 15:47:3410087browse

jquery changeEventThe problem of not triggering
First define 2 inputs, the purpose is to copy the value of input1 to input2

<input name="input1" />
<input name="input2" />

The code is as follows

$(":input[name=&#39;input1&#39;]").bind(&#39;change focus blur&#39;, function() {
$(":input[name=&#39;input2&#39;]").val($(this).val());
});

jquery's change is not triggered.
It's not because the code is wrong, it's because input1 is directly assigned by jquery.

$(":input[name=&#39;input1&#39;]").val(&#39;AAAAAA&#39;);

This way it cannot be triggered.
The temporary solution is,

$(":input[name='input1']").focus();
$(":input[name=&#39;input1&#39;]").val(&#39;AAAAAA&#39;);
$(":input[name='input1']").blur();

================================== ===========
Please give me a better way!!!!! Or how to solve it.............

= ==================================================

Attach the complete code.........the problem has not been solved for the time being

$(function() {
$(":input[name=&#39;input1&#39;]").bind(&#39;change focus blur&#39;, function() {
$(":input[name=&#39;input2&#39;]").val($(this).val());
});
});
var setvalue = function() {
$(":input[name=&#39;input1&#39;]").val(&#39;AAAAAA&#39;);
}
<input name="input1" />
<input name="input2" />
赋值测试

================ ====================================

Tested with no problems

$(function(){
 
 $(":input[name=&#39;bbb&#39;]").val(&#39;aAAAAAAAA&#39;);
 
 $(":input[name=&#39;aaa&#39;]").bind("change focus blur",function(){
 
  alert($(this).val());
 
  $(":input[name=&#39;bbb&#39;]").val($(this).val());
 
 });
 
})

Please see the complete code in the supplement to the question. Your test cannot copy the value

The value of input1 is assigned directly by jquery. According to your logic, the two values ​​are Same, then just assign a value to input2 at the same time.

This is a simplified code. In the actual project, because the first module cannot be changed, it can only be solved through monitoring

You said you can't change the first module, so what's going on with your code?

$(":input[name='input1']").focus();
$(":input[name=&#39;input1&#39;]").val(&#39;AAAAAA&#39;);//这一段相当于你说的第一个模块,既然你能上下加上两行代码,那为何不直接
$(":input[name='input2']").val('AAAAAA');
$(":input[name='input1']").blur();

Can you not worry about this problem?
This is a temporary solution, temporary, you need to change it back Yes,
Otherwise, why would I ask this question!!! Who doesn’t know how to assign values ​​directly?

$("input").first().keyup(function(){
$(this).next().val($(this).val());
});

The event cannot be captured at all, because the keyboard will not be pressed on input1 at all,
input1 The value is loaded through ajax. Now I want to copy it to input2 through events instead of in the ajax code

Just change

$("input").first().change(function(){   $(this).next().val($(this).val());});
// ajax改值
$("input").first().change()

The above is the detailed content of jquery: solution to the problem that the change event does not trigger. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn