2013-05-07 29 views
0

我有一個腳本調用數據庫來獲取自動完成數據。總和無法添加和刪除動態域

一旦選擇了特定的自動完成數據(項目名稱),則會填入其他一些字段,即:項目代碼,價格&數量。

當數量發生變化時,行總數字段將更改爲「數量*價格」總數。

有一個選項可以進行添加行動態和自動完成每個以相同的方式...有一個總計,將每一行總數加起來。

我的問題是,只有當數量發生變化時總計纔會發生變化,它應該加載到每一行的價格焦點上 - 同樣,刪除一行並不會從總計中刪除該特定行總額,如果你改變了第一行數量(只有第一行受到影響),它將總數重新設置爲現在添加的行數......請有人看看我的代碼。

現場版是在 http://cardoso.co.za/form/

您還可以得到其中的文件太多,如果你瀏覽到 http://cardoso.co.za/form/form.zip

任何及所有幫助是極大的讚賞!這裏

編輯是一個從整個腳本一些代碼:

var $itemsTable = $('#itemsTable'); 
var rowTemp = [ 
'<tr class="item-row">', 
'<td><a id="deleteRow"><img src="images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>', 
'<td><input name="itemType" class="itemType" id="itemType" tabindex="1" style="width:350px;"/></td>', 
'<td align="center"><input name="itemCode" class="itemCode" id="itemCode" readonly="readonly" style="width:60px;" tabindex="-1"/></td>', 
'<td align="center"><input name="itemQty" class="itemQty" id="itemQty" tabindex="2" style="width:40px;" maxlength="4" value=""/></td>', 
'<td width="14%" align="center"><input name="itemPrice" class="itemPrice" id="itemPrice" readonly tabindex="-1"/></td>', 
'<td width="17%" align="right"><input name="itemTotal" class="itemTotal" id="itemTotal" readonly tabindex="-1"/></td>', 
'</tr>' 
].join(''); 
$('#addRow').bind('click',function(){ 
var $row = $(rowTemp); 
var $itemType = $row.find('#itemType'); 
var $itemCode = $row.find('.itemCode'); 
var $itemPrice = $row.find('.itemPrice'); 
var $itemQty = $row.find('.itemQty'); 
var $itemTotal = $row.find('.itemTotal'); 
if ($('#itemType:last').val() !== '') { 
$row.find('#itemType').autocomplete({ 
source: 'item-data.php', 
minLength: 1, 
select: function(event, ui) { 
$itemType.val(ui.item.itemType); 
$itemCode.val(ui.item.itemCode); 
$itemPrice.val(ui.item.itemPrice); 
$itemTotal.focus().val(ui.item.itemPrice); 
$itemQty.focus().val(1); 
$itemQty.keyup(function() { 
var Quantity = $itemQty.val(); 
var Prices = $itemPrice.val(); 
var ItemsTotal = Quantity * Prices; 
$itemTotal.val(ItemsTotal.toFixed(2)); 
var Tsum = 0; 
$('.itemTotal').each(function() { 
if(!isNaN(this.value) && this.value.length!=0) { 
Tsum += parseFloat(this.value); 
$('#toTally').val(Tsum.toFixed(2)) 
} 
}); 
}); 
return false; 
} 
}).data("autocomplete")._renderItem = function(ul, item) { 
return $("<li></li>") 
.data("item.autocomplete", item) 
.append("<a>" + item.itemType + "</a>") 
.appendTo(ul); 
}; 
$('.item-row:last', $itemsTable).after($row); 
$('#minusRow').show(); 
$('#resetTable').show(); 
$($itemType).focus(); 
} 
return false; 
}); 

我已經實現了陣,反而會很快張貼此,一旦我得到它的無卡阻帶的ID和類工作。

+1

請發表您的代碼在這裏,所以未來的堆棧溢出的用戶可以參考同樣的問題。你的代碼可能會在你的服務器上發生變化 – Sablefoste 2013-05-07 14:21:35

+0

我並不擅長jQuery,但我認爲你的每個輸入字段都使用同一個ID,並且價格是錯誤的。此外,你的jQuery總結了這個ID的val(),它只獲得第一行。應該有一個像數組我想 – 2013-05-07 14:23:47

+0

Thenks黑貂 - 我試圖添加我的代碼,但它太長了:(我會嘗試連接它。 – user2265402 2013-05-07 14:25:52

回答

0

我通過執行以下操作來解決問題:

  (function($) { 
     var pounter = 1; 
     $(document).ready(function(){ 
      var qounter = 1; 
      var trounter = 1; 

       //Count each instance and add number to Name 
       $row.find('._ext_price_total_html').each(function() { 
       $(this).attr({ 
       'name': function(_, name) { 
        return name + pounter }, 
        }); 
       pounter++; 
       }) 
      $row.find('.quantitys').each(function() { 
       $(this).attr({ 
       'name': function(_, name) { 
        return name + qounter }, 
        }); 
       qounter++; 
       }) 
      $('#quantity', function(){ 
       var Tsum = 0; 
       $('[id^="_ext_price_total_html"]').each(function() { 
        if(!isNaN(this.value) && this.value.length!=0) { 
        Tsum += parseFloat(this.value); 
        } 
         $('.toTally').val(Tsum.toFixed(2))               
       }) 
       }) 
       $('#quantity').change(function(){ 
       var Tsum = 0; 
       $('[id^="_ext_price_total_html"]').each(function() { 
        if(!isNaN(this.value) && this.value.length!=0) { 
        Tsum += parseFloat(this.value); 
        } 
         $('.toTally').val(Tsum.toFixed(2))        
        }) 
       }) 
       $('#quantity').keyup(function(){ 
       var Tsum = 0; 
       $('[id^="_ext_price_total_html"]').each(function() { 
        if(!isNaN(this.value) && this.value.length!=0) { 
        Tsum += parseFloat(this.value); 
        } 
         $('.toTally').val(Tsum.toFixed(2)) 
       }) 
       }) 
     //adjust/subtract from total when removeing row 

     $('#deleteRow').live('click',function(){ 
var delAsk = confirm('Remove this item?'); 
     if (delAsk) 
     { 
     $(this).parents('.jshop_prod_cart').remove(); 
      var Tsum = 0; 
       $('[id^="_ext_price_total_html"]').each(function() { 
        if(!isNaN(this.value) && this.value.length!=0) { 
        Tsum += parseFloat(this.value); 
        } 
         $('.toTally').val(Tsum.toFixed(2)) 

       }) 
     } 
     else 
     { 
      ('cancel'); 
      } 
       }); 
+0

您可以通過點擊複選標記來接受自己的答案。請不要在標題中加入「已解決」。 – Antony 2013-05-16 11:15:18