Home > Article > Web Front-end > jquery: solution to the problem that the change event does not trigger
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='input1']").bind('change focus blur', function() { $(":input[name='input2']").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='input1']").val('AAAAAA');
This way it cannot be triggered.
The temporary solution is,
$(":input[name='input1']").focus(); $(":input[name='input1']").val('AAAAAA'); $(":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='input1']").bind('change focus blur', function() { $(":input[name='input2']").val($(this).val()); }); }); var setvalue = function() { $(":input[name='input1']").val('AAAAAA'); } <input name="input1" /> <input name="input2" /> 赋值测试
================ ====================================
Tested with no problems
$(function(){ $(":input[name='bbb']").val('aAAAAAAAA'); $(":input[name='aaa']").bind("change focus blur",function(){ alert($(this).val()); $(":input[name='bbb']").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='input1']").val('AAAAAA');//这一段相当于你说的第一个模块,既然你能上下加上两行代码,那为何不直接 $(":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!