내가하려는 것은 각 선택 상자에서 선택한 옵션을 잡고 '제출'을 클릭하여 '저장된'단락에 저장하는 것입니다.선택한 텍스트를 가져와 표시하는 방법은 무엇입니까?
<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>
자바 스크립트 :
여기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()
을하지만, 마지막에만 여기에
내 HTML입니다 selectbox의 텍스트가 "stored"단락에 표시되고 있습니다. 무엇이 잘못 되었습니까? 변화를function submit() {
for (var i=0; i<select.length; i++) {
var e = document.getElementById("select" + (i + 1));
var storedNumbers = e.options[e.selectedIndex].text;
document.getElementById("select" + (i + 1)).text;
document.getElementById("stored").innerHTML=storedNumbers;
}
그리고 당신은 즉으로 발생하는 문제 ...? 무슨 일이 일어나고 있지 않아? 어떤 오류가 발생합니까? 콘솔로 디버깅 해 보셨습니까? – j08691
@ j08691 오류는 발생하지 않지만 저장된 숫자는 내 단락에 표시되지 않습니다. 그래서 나는 무엇이 문제인지 모르겠다. .. –