我想要做的是从每个选择框中选取选项,单击“提交”并将它们存储在“存储”段落中。如何获取选定的文本并显示它们?
这里是我的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我没有收到任何错误,但存储的数字也没有显示在我的段落中。所以我不知道是什么问题.. –