Home  >  Article  >  Web Front-end  >  Solution to the invalid input change event in jquery

Solution to the invalid input change event in jquery

黄舟
黄舟Original
2017-06-27 09:44:515295browse

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>"
+&#39;<td><input type="number" class="product_info" name="setPrice" id="setPrice" onchange="setprice()" value="" /></td>&#39;
+&#39;<td><input type="number" class="product_info" name="setStock" id="setStock" value="" /></td>&#39;
+"</tr>");

$(&#39;input[name=setStock]&#39;).on("change",function(){
   console.log($(&#39;[name=setStock]&#39;).val());
   $(&#39;[name=stock]&#39;).val($(&#39;#setStock&#39;).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>"
+&#39;<td><input type="number" class="product_info" name="setPrice" id="setPrice" onchange="setprice()" value="" /></td>&#39;
+&#39;<td><input type="number" class="product_info" name="setStock" id="setStock" value="" /></td>&#39;
+"</tr>");

This code is in a Button started,
but

$(&#39;input[name=setStock]&#39;).on("change",function(){   
console.log($(&#39;[name=setStock]&#39;).val());   
$(&#39;[name=stock]&#39;).val($(&#39;#setStock&#39;).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!

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