这是很难解释声明的值,你可以看到一个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
航运输入在哪里? –
航运...我很抱歉它被称为旅行。而小计正确地广告了我所有的东西,我只是不确定如何将“旅行”添加到总计中。 –