我想要做的是從每個選擇框中選取選項,單擊「提交」並將它們存儲在「存儲」段落中。如何獲取選定的文本並顯示它們?
這裏是我的HTML:
<body>
<div id="container">
<button id="button" onclick="rolldices()">Roll dices</button>
<span id="dice1">0</span>
<span id="dice2">0</span>
<span id="dice3">0</span>
<span id="dice4">0</span>
<span id="dice5">0</span>
<span id="dice6">0</span>
<br><br><br><br>
<select id="select1">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select>
<select id="select2">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select>
<select id="select3">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select>
<select id="select4">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select>
<select id="select5">
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select>
<select id="select6">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
<button id="submit" onclick="submit()">Submit your answer</button>
<br><br>
<p id="correct">Correct numbers guessed: </p>
<p id="stored"></p>
</div>
</body>
</html>
的Javascript:
var numbers = [ '1', '2', '3', '4', '5', '6' ];
var dices = [ 'dice1', 'dice2', 'dice3', 'dice4', 'dice5', 'dice6' ];
var select = [ 'select1', 'select2', 'select3', 'select4', 'select5', 'select6']
function rolldices() {
for (var diceindex=0; diceindex<dices.length; diceindex++) {
var dice_value = Math.floor((Math.random()*numbers.length));
document.getElementById("dice" + (diceindex + 1)).innerHTML=numbers[dice_value];
}
} // end of rolldices()
這是我在解決問題的嘗試:
function submit() {
for (var selectindex=0; selectindex<select.length; selectindex++) {
var e = document.getElementById("select" + (selectindex + 1));
var storedNumbers = e.options[e.selectedIndex].text;
document.getElementById("select" + (selectindex + 1)).text;
document.getElementById("stored").innerHTML=storedNumbers;
}
} // end of submit()
這個工程那種,但只有最後選擇框的文本正在顯示在「存儲」段落中。什麼是錯誤的?
而你與遇到的問題是......?什麼情況不會發生?你會得到什麼錯誤?你用控制檯調試過了嗎? – j08691
@ j08691我沒有收到任何錯誤,但存儲的數字也沒有顯示在我的段落中。所以我不知道是什麼問題.. –