2012-12-09 127 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

接受答案,沒問題:) –