您的位置:首页 > Web前端 > JQuery

jquery 中 change input 事件失效

2015-08-17 16:37 609 查看
今天工作的时候 遇到了用jquery选择 input输入框, 监听input中的内容变化,然后将input中的内容获取到,赋值给另一个input输入框。这种逻辑本来很简单的。代码如下:
$(".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());
});
我是尽量确保前面一段代码在后一段代码之前加载了,但是通过这种方式 选择不到 id=setStock的input输入框。..............十分钟后我发现
$(".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());});我是反在了script标签的尾部了。所以按钮没有点击之前,这段代码就已经执行了,此时上面的
$(".showProductInfo").append()还没有执行!!!
原因找到了!
为了找到原因我还专门把第一个setprice()方法写到html中,变成html事件处理事件。 唉低级错误!!记录一下!共勉!

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: