作爲JavaScript的新手,我認爲我有一個簡單的問題。對於循環語法錯誤 - 一次點擊貫穿整個循環
- 編輯我想一個事件的發生(兩個按鈕出現的)用戶點擊從下拉框3次的選項之後。我最初認爲for循環會幫助我做到這一點。我還希望最後的選擇(「無」或其他選項)顯示警告,並且不會影響您留下的點擊次數。希望更有意義。
任何幫助將不勝感激。
var question1 = new Array();
window.onload = function() {
var eSelect = document.getElementById('question1');
var optOtherReason = document.getElementById('displayresponse');
var options = document.getElementsByTagName("option");
eSelect.onchange = function() {
for (var i=0; i<3; i++) {
if (eSelect.selectedIndex !== 3) {
var li = document.createElement("li");
li.innerHTML = options[eSelect.selectedIndex].innerHTML;
var ol = document.getElementById("appendedtext");
ol.appendChild(li);
question1.push(li.innerHTML);
var x= document.getElementById("display");
x.innerHTML=question1;
}
}
if (eSelect.selectedIndex == 3) {
i=0;
optOtherReason.style.display = 'block';
}
}
insertbutton= "block";
displaybutton = "block";
}
<select id="question1" name="question">
<option value="x">Reason1</option>
<option value="y">Reason2</option>
<option value="other">Otherreason</option>
<option value="none">None</option>
</select><br>
<div id="insertbutton" style="display:none;">
<input type="button" value="Save" onclick="insert();">
</div>
<div id="generatebutton" style="display:none;">
<input type="button" value="Show Drink" onClick="generate();">
</div>
<div id="displayresponse" style="display:none;">If you did not see a choice here, you may search for other sites.</div>
<ol id="appendedtext"> </ol>
<div id="display"></div>
我不明白你在問什麼......當然,循環將運行三次,但我想這不是驚喜嗎?首先,你是否通過調試輸出檢查你的第一個if條件是否爲真?例如,在if(之後檢查您的瀏覽器的JS控制檯)之前通過'console.log(eSelect.selectedIndex,eSelect.selectedIndex!== 3)'。 – CBroe
我正在尋找一種方式來啓動一個事件(按鈕出現)後,用戶從下拉菜單中點擊三次選項。我的印象是一個循環會有所幫助。 – mo1248