2012-12-09 122 views
-1

我正在使用jquery.calculate创建一个进销存系统。jQuery计算问题

 var newIDSuffix = 2; 
    $(".invoice_items").delegate('#add-row', 'click', function() { 

     var lastRow = $("tr.lastrow"); 
     var cloned = $("tr.default").clone().removeAttr('class'); 

     cloned.find('td#total_item_1').each(function() { 
      $(this).attr('id', 'total_item_'+2); 
     }); 

     cloned.find('input, select').each(function() { 
      var id = $(this).attr('id'), 
      name = $(this).attr('name'); 

      id = id.substring(0, id.length-1) + newIDSuffix; 
      $(this).attr('id', id); 
     }); 

     cloned.insertBefore(lastRow).find('input:text').val(''); 
     newIDSuffix++; 
    }); 

     // update the plug-in version 
     $("#idPluginVersion").text($.Calculation.version); 

     // bind the recalc function to the quantity fields 
     $("input").bind("keyup", recalc); 
     // run the calculation function now 
     recalc(); 

     $("input").bind("keyup", recalc); 
     // run the calculation function now 
     recalc(); 

这是我迄今为止所做的。 http://jsfiddle.net/aliharis/VXZe8/

问题是一旦我动态添加一行,并且当我输入值时,它不会更新金额列和总金额。但是,如果我为动态添加的行输入值并返回到第一行并输入值,则会进行包括动态添加字段的计算。

我找不出什么问题。

+1

而是绑定的应该使用'。对()',http://api.jquery.com/on/。 –

回答

0

添加一行:

cloned.bind("keyup", recalc); 

这一行之后:

cloned.insertBefore(lastRow).find('input:text').val(''); 

到click事件的新元素绑定。

..或者选择使用。对(),而不是绑定在这里:

$("input").on("keyup", recalc); 
+0

谢谢。有用。 :) –

+0

接受答案,没问题:) –