When I was working today, I encountered the problem of using jquery to select the input input box, monitor the content changes in the input, and then obtain the content in the input and assign it to another input input box.
This logic is originally very simple. The code is as follows:
$(".showProductInfo").append("<tr>" +"<td></td>" +"<td>批量设置</td>" +'<td><input type="number" class="product_info" name="setPrice" id="setPrice" onchange="setprice()" value="" /></td>' +'<td><input type="number" class="product_info" name="setStock" id="setStock" value="" /></td>' +"</tr>"); $('input[name=setStock]').on("change",function(){ console.log($('[name=setStock]').val()); $('[name=stock]').val($('#setStock').val()); });
I try to ensure that the previous piece of code is loaded before the latter piece of code, but in this way, the input box with id=setStock cannot be selected.
............
Ten minutes later
I found that
$(".showProductInfo").append("<tr>" +"<td></td>" +"<td>批量设置</td>" +'<td><input type="number" class="product_info" name="setPrice" id="setPrice" onchange="setprice()" value="" /></td>' +'<td><input type="number" class="product_info" name="setStock" id="setStock" value="" /></td>' +"</tr>");
This code is in a Button started,
but
$('input[name=setStock]').on("change",function(){ console.log($('[name=setStock]').val()); $('[name=stock]').val($('#setStock').val());}); 我是反在了script标签的尾部了。所以按钮没有点击之前,这段代码就已经执行了,此时上面的 $(".showProductInfo").append()还没有执行!!!
The reason was found! In order to find the reason, I also specifically wrote the first setprice() method into html and turned it into htmlevent handling event. Alas, low-level error! ! Record it! mutual encouragement!
The above is the detailed content of Solution to the invalid input change event in jquery. For more information, please follow other related articles on the PHP Chinese website!