我有一個函數,它應該從站點獲取所有輸入,將它們的值乘以它們的數量,然後用結果替換內部的字符串。問題是該函數只執行一次,或者有時從未執行過。它的代碼是:Javascript函數只能運行一次
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 }?>
你能解釋一下我在哪裏犯了一個錯誤?
你可以爲你的問題添加一個jsfiddle嗎? – aravind
函數是如何調用的? –
該函數被調用 type;?>「> –