我有一個問題,我希望下面的HTML被循環並總結爲一個總數。這是行得通的。動態多選和動態值
不過,我也想能夠:
- 輸出的每個選擇具有相應標籤爲 儘快地選擇,因爲它已被選中,並保存在每個選擇框中每個選定 選項的值
我在如何獲得相應的標籤和選擇框選擇的選項,以便我可以在稍後階段使用它們有麻煩。
例如: 我想知道:
- 選擇哪些選項
- 什麼是所選選項的名稱
- 什麼是所選選項的值
- 什麼是每個選定選項的相應標籤
HTML:
<div id = "form">
<form id = "test">
<label>Label 1/label>
<select class='pricebox' name='box1' id='box1' onchange='code(1)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='2500'>2500 Text</option>
</select>
<br />
<label>Label 2/label>
<select class='pricebox' name='box2' id='box2' onchange='code(2)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='2500'>2500 Text</option>
</select>
<br />
<label>Label 3/label>
<select class='pricebox' name='box3' id='box3' onchange='code(3)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='2500'>2500 Text</option>
</select>
<br />
<label>Label 4/label>
<select class='pricebox' name='box4' id='box4' onchange='code(4)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='2500'>2500 Text</option>
</select>
<br />
<label>Label 5/label>
<select class='pricebox' name='box5' id='box5' onchange='code(5)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='7000'>7000 Text</option>
</select>
<br />
<label>Label 6/label>
<select class='pricebox' name='box6' id='box6' onchange='code(6)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='300'>300 Text</option>
</select>
<br />
<label>Label 7/label>
<select class='pricebox' name='box7' id='box7' onchange='code(7)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
</select>
<br />
<label>Label 8/label>
<select class='pricebox' name='box8' id='box8' onchange='code(8)'>
<option value='0'>Choose</option>
<option value='100'>100 Text</option>
<option value='250'>250 Text</option>
<option value='500'>500 Text</option>
<option value='1000'>1000 Text</option>
<option value='2500'>2500 Text</option>
</select>
<br />
<p id="demo"></p>
</form>
</div>
JS:
function code() { // forget the parameter
var elements = document.getElementsByClassName("pricebox");
var names = '';
var datvalue = 0;
var i = 0;
while(i<elements.length) {
names = elements[i].name;
elval = parseFloat(elements[i].value);
datvalue = datvalue + elval;
i++;
document.getElementById("demo").innerHTML = "<b>Total: " + datvalue + " SEK</b>";
}
}