我有一个文本框的乘法运算,但是,当我添加一个新的文本框时,该函数将不会执行。我制作的js函数只执行了第一个文本框。很抱歉问这个问题,但我对jsquery/javascript还很陌生。
这是我的工作脚本:JS FIDDLE
What it does is, I have a table and 2 TR on it. it multiply the cost(3rd td) and the quantity(fourth td) and send the results to the line total (fifth td). Then I have a button, that will add another set of tr to the existing table, however, the multiplication wont work on the newly added tr.
你能在这里帮我吗?
TIA,
尼米兹
只需将这两行添加为.click(…)处理程序的最后两行:
$(".qty :input").off('keyup',byPrice).on('keyup',byPrice)
$(".price :input").off('keyup',byQty).on('keyup',byQty);
这里是更新的JSFiddle
原因是,您必须为新创建的行重新注册事件侦听器。您可以使用相同的选择器,但必须"关闭"以前的事件侦听器。
更新
另一种方法是,您只能在新创建的行上绑定事件:这是的另一个JSFiddle
您必须将新创建的行的内容放入某个变量中:
$('#myinvoice tr:last').after($newRow = $('<tr><td>...'));
然后在$newRow:中为选择器添加事件
$(".qty :input",$newRow).keyup(byPrice);
$(".price :input",$newRow).keyup(byQty);
我会使用第二个