我有一个函数应该从站点获取所有输入,添加它们的值乘以它们的数量,然后用结果替换里面的字符串。问题是函数只执行一次或有时它从未执行过。它的代码是:
function calcTotal() {
var inputs = document.getElementsByTagName("input");
var total = 0;
var priceString = null;
var e = null;
var price = null;
for (var i = 0; i < inputs.length; i++) {
e = inputs[i].value;
priceString = "hidden_" + inputs[i].name;
if (e.match(/[0-9\.]/)) {
var price = document.getElementById(priceString).innerHTML;
total = total + parseFloat(e) * parseFloat(price);
}
}
var totalString = document.getElementById("total");
var stringToDisplay = total.toFixed(2);
totalString.innerHTML = stringToDisplay.toString();
}
该函数应替换此范围:<span id="total">0.00</span>
通过代码调用每个输入的更改函数:
<?php foreach($allPremium as $premiumItem){?>
<?php $ceny[$premiumItem->type]=$premiumItem->price;?>
<div class="premium-pack-item">
<?php echo $premiumItem->name;?>
<br />
<?php echo $premiumItem->description;?>
<br />
<?php echo "€ ".number_format($premiumItem->price, 2);?>
<br />
<span id="hidden_amount_<?php echo $premiumItem->type;?>" style="display:none"><?php echo $premiumItem->price;?></span>
<input onkeypress="calcTotal()" type="number" size=1 name="amount_<?php echo $premiumItem->type;?>">
</div>
<?php }?>
你能解释一下我犯了哪个错误吗?
答案 0 :(得分:0)
我首先会检查您是否使用开发人员工具收到任何javascript错误,这可能会揭示真正原因。