我需要始终填写#totalcost。有一些 javascript 计算并将结果写入#total cost。我试图将它放在 JSFiddle 上,但触发函数 onload 没有成功。对于 .number 输入字段的每次更改,将 .localTotal 设置为 localTotal * number 值。总而言之,我需要 .localTotal 和 #totalcost 始终填写
.localTotal 与 .price*.quantity 和 #totalcost 与所有 .localTotals 的总和
这是 HTML
<div class="row"> <div class="col-xs-5">
<span class="glyphicon glyphicon-remove remove" style="cursor:pointer;"></span> Ventura L15220</div>
<div class="col-xs-3 price">2020.20</div>
<div class="col-xs-4"><input type="number" class="form-control quantity" value="1"></div>
<div class="localTotal"></div></div>
<div class="row">
<div class="col-xs-5"><span class="glyphicon glyphicon-remove remove" style="cursor:pointer;"></span> Ventura L15220</div>
<div class="col-xs-3 price">30300.20</div><div class="col-xs-4"><input type="number" class="form-control quantity" value="1"></div>
<div class="localTotal"></div></div>
<div class="col-xs-4 head " id="totalcost"></div>
和 javascript
function changeQuantity(){
$(".quantity").trigger("change");
console.log("done")
$('.quantity').change(calculateTotal);
function calculateTotal(){
$quantity=$(this);
$price=$quantity.closest('.row').find('.price').text();
$quantityValue=$quantity.val();
$localTotal=$quantity.closest('.row').find('.localTotal');
$localTotalValue=$quantityValue*$price;
$quantity.closest('.row').find('.localTotal').text($localTotalValue);
console.log($localTotalValue);
var sumToTotal = 0;
var totalSum=document.getElementById('totalcost');
$('.localTotal').each(function(){
sumToTotal += parseFloat($(this).text());
});
totalSum.innerHTML=sumToTotal+' грн';
}
}
changeQuantity() 在加载时触发,但尽管 $(".quantity").trigger("change"); #totalcost 和 .localTotal 未填写:c