我無法弄清楚這一點。我可以使用一些幫助,因爲我的JavaScript知識很少。添加選擇輸入菜單的值
我想使用多個選擇菜單,並實時添加關閉所選項目的值,並從固定數字中減去。
<form>
<section id="extra-features">
<select name="450_1" id="450_1" class="form-control" onchange="updateTotals()" >
<option selected="selected"></option>
<option value="larry,13">larry</option>
<option value="dex,12">dex</option>
<option value="ryder,18">ryder</option>
</select>
<select name="450_2" id="450_2" class="form-control" onchange="updateTotals()">
<option selected="selected"></option>
<option value="bob,4">bob</option>
<option value="jim,6">jim</option>
<option value="stew,7">stew</option>
</select>
</section>
</form>
<p id="total"></p>
<p id="balance"></p>
這裏是我的javascript
function updateTotals() {
var inputs = document.getElementById('extra-features').getElementsByTagName('select');
var sum = 0;
var bsum = 2000000;
for (var i = 0, num = inputs.length; i < num; i++) {
if (inputs[i].change) {
sum += parseInt(inputs[i].getAttribute('value').split(",")[1]);
bsum -= parseInt(inputs[i].getAttribute('value').split(",")[1]);
}
}
document.getElementById('total').innerHTML = sum;
document.getElementById('balance').innerHTML = bsum;
}
var section = document.getElementById('extra-features');
var inputs = section.getElementsByTagName('select');
for (var i = 0, num = inputs.length; i < num; i++) {
inputs[i].addEventListener('change', updateTotals);
}
這裏是我的小提琴JSFiddle
什麼是你的問題? – forgivenson
它不工作。我的問題是我做錯了什麼。 – Rooster
你爲什麼要將選擇上的更改事件綁定兩次? – j08691