我有一个动态表单,其中的行通过 jquery 添加到 dom,并且总价格似乎表现得很奇怪
我可以添加和删除动态表单中的最后一行并计算单价总和,但calculateSumNoTax()函数似乎只在由php输出的第一行被触发,每隔一行通过jquery附加到dom不要触发calculateSumNoTax();
我在这里做错了什么?
JS:
$(document).ready(function() {
$("a[name='addline']").click(function(){
$("input[name='counter']").val( Number($("input[name='counter']").val()) + 1 );
var i = $("input[name='counter']").val();
$('#quote > tbody:last').append('<tr id="row'+i+'"><td>'+i+'</td><td><input type="text" name="description_'+i+'" value="" /></td><td><input type="text" name="quantity_'+i+'" value="1" /></td><td><input type="text" name="unit_price_'+i+'" class="unit_price" value="" /></td><td><select name="tax_percentage_'+i+'"><option value="0" selected="selected">0</option><option value="19.6">19.6%</option></select></td></tr>');
event.preventDefault();
});
$("a[name='removeline']").click(function(){
var i = $("input[name='counter']").val();
if(i > 1){
$("tr[id='row"+i+"']").remove();
$("input[name='counter']").val( Number($("input[name='counter']").val()) - 1 );
}
event.preventDefault();
});
$(".unit_price").keyup(function() {
$(this).keyup(function(){
calculateSumNoTax();
});
});
});
function calculateSumNoTax() {
var sum = 0;
//iterate through each textboxes and add the values
$(".unit_price").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#notax").html(sum.toFixed(2));
}
I can add and remove the last line in my dynamic form and calculate the sum of the unit prices but the calculateSumNoTax() function seems to only be fired bu the first line which is output by php, every other line appended to the dom via jquery dont fire calculateSumNoTax();
What am I doing wrong here ?
JS :
$(document).ready(function() {
$("a[name='addline']").click(function(){
$("input[name='counter']").val( Number($("input[name='counter']").val()) + 1 );
var i = $("input[name='counter']").val();
$('#quote > tbody:last').append('<tr id="row'+i+'"><td>'+i+'</td><td><input type="text" name="description_'+i+'" value="" /></td><td><input type="text" name="quantity_'+i+'" value="1" /></td><td><input type="text" name="unit_price_'+i+'" class="unit_price" value="" /></td><td><select name="tax_percentage_'+i+'"><option value="0" selected="selected">0</option><option value="19.6">19.6%</option></select></td></tr>');
event.preventDefault();
});
$("a[name='removeline']").click(function(){
var i = $("input[name='counter']").val();
if(i > 1){
$("tr[id='row"+i+"']").remove();
$("input[name='counter']").val( Number($("input[name='counter']").val()) - 1 );
}
event.preventDefault();
});
$(".unit_price").keyup(function() {
$(this).keyup(function(){
calculateSumNoTax();
});
});
});
function calculateSumNoTax() {
var sum = 0;
//iterate through each textboxes and add the values
$(".unit_price").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#notax").html(sum.toFixed(2));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当动态添加新行时,事件侦听器(keyup 函数)不会附加到它。
它必须单独完成。事件侦听器可以在“线路”上线(创建)后立即添加到“线路”中。
When a new line is added dynamically, the event listener (keyup function) is not attached to it.
It has to be done separately. Event listeners can be added to the "line" as soon as it is live (created).
我刚刚在这里找到了答案:动态添加输入上的 Jquery .keypress
更改为:
$(".unit_price").live('keypress', function(e){
$(this).keyup(函数(){
计算SumNoTax();
});
});
I just found the answer here : Jquery .keypress on a dynamically added input
Changed to :
$(".unit_price").live('keypress', function(e){
$(this).keyup(function(){
calculateSumNoTax();
});
});