Home > Web Front-end > JS Tutorial > body text

Solution to the invalid input change event in jquery

黄舟
Release: 2017-06-27 09:44:51
Original
5334 people have browsed it

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());
});
Copy after login

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>");
Copy after login

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()还没有执行!!!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template