【发布时间】:2018-12-01 09:33:54
【问题描述】:
我正在尝试循环选择表单以在单击时增加所选选项的值。我做了一些搜索,但我没有找到我需要的东西..这里是 html
var counter = 0;
function myFn(){
var myOptionIndex = document.getElementById('selectForm').selectedIndex;
var myOptionValue = document.getElementById('selectForm').value;
var myOption = document.getElementById('selectForm').options;
for (var i = 0; i < myOption[myOptionValue].value; i++) {
counter++;
}
document.getElementById('results').innerHTML = myOption[myOptionIndex].text
+ " has been selected " + counter + " times";
}
<form action="" id="voteForm">
<select name="select-form" id="selectForm">
<option value="1"> Option 1</option>
<option value="1"> Option 2</option>
</select>
<button type="button" id="castVote" onclick="myFn();">Cast Your
Vote
</button>
</form>
<div id="results"></div>
【问题讨论】:
-
您是否尝试在每次单击时将值增加 1。
-
更改选择的选项值。
document.getElementById('selectForm').options[myOptionIndex].value = 'new value'
标签: javascript html forms loops increment