這是很難解釋聲明的值,你可以看到一個DEMO HERE添加到另一個函數
我有一個產品表中動態創建/刪除新的產品線。我還有一個總計表,將每條線的總數加起來。
在這個總計框中,我有一個旅行箱,我想添加到總計,但我遇到的問題是旅行輸入在表格之外,總計所有值。我可以用新的總數替換總數,但我似乎無法稱爲小計,增加旅行並輸出總計。
HTML
<table class="order-details">
<tbody>
<tr>
<td><input type="text" value="" name="" placeholder="Work Description" class="wei-add-field description 1"/></td>
<td><input type="text" value="" name="" placeholder="QTY" class="wei-add-field quantity 1" /></td>
<td><input type="text" value="" name="" placeholder="$0.00" class="wei-add-field unit-price 1"/></td>
<td><input type="text" value="" name="" placeholder="$0.00" class="wei-add-field price-total 1" id=""/></td>
<td> </td>
</tr>
</tbody>
</table>
<div class="wei-add-service"><a href="#" class="button-secondary wei-add-service-button">Add Item</a></div>
<table class="wei-add-totals">
<tr>
<td width="50%">Sub Total</td>
<td width="50%" class="wie-add-subtotal"> </td>
</tr>
<tr class="alternate travel">
<td>Travel</td>
<td><input type="text" value="" placeholder="0.00" class="wei-add-field travel" /></td>
</tr>
<tr>
<td>Taxes</td>
<td><input type="text" value="" placeholder="0.00" class="wei-add-field wie-total-taxes" id="wei-disabled" disabled/> </td>
</tr>
<tr class="alternate total">
<td>Total</td>
<td><input type="text" value="" placeholder="0.00" class="wei-add-field wie-grand-total" id="wei-disabled" disabled/></td>
</tr>
</table>
的Javascript
var counter = 1;
var testArray = [ 2,3,4,5];
jQuery('a.wei-add-service-button').click(function(event){
event.preventDefault();
counter++;
var newRow = jQuery('<tr><td><input type="text" class="wei-add-field description ' + counter + '"/></td><td><input type="text" class="wei-add-field quantity ' + counter + '" /></td><td><input type="text" class="wei-add-field unit-price ' + counter + '"/></td><td><input type="text" value="" name="" placeholder="$0.00" class="wei-add-field price-total ' + counter + '" id=""/></td><td><a href="#">X</a></td></tr>');
jQuery('table.order-details').append(newRow);
});
jQuery('table.order-details').on('click','tr a',function(e){
e.preventDefault();
var table = $(this).closest('table');
jQuery(this).parents('tr').remove();
reCalculate.call(table);
});
jQuery('table.order-details').on("keyup", "tr", reCalculate);
function reCalculate() {
var grandTotal = 0;
jQuery(this).closest('table').find('tr').each(function() {
var row = jQuery(this);
var value = +jQuery(".unit-price", row).val();
var value2 = +jQuery(".quantity", row).val();
var total = value * value2;
grandTotal += total;
jQuery(".wei-add-field.price-total", row).val('$' + total.toFixed(2));
});
jQuery(".wie-add-subtotal").text('$' + grandTotal.toFixed(2));
}
您的演示,並在這個問題上你的代碼*不符合*。由於'.wie-add-subtotal'不是'input'字段,因此請使用'jQuery(「。wie-add-subtotal」).text'而不是'jQuery(「。wie-add-subtotal」).val' 。 – TbWill4321
航運輸入在哪裏? –
航運...我很抱歉它被稱爲旅行。而小計正確地廣告了我所有的東西,我只是不確定如何將「旅行」添加到總計中。 –