作为javascript的新手,我在编写If语句时遇到了麻烦,第四轮之后发生了一个事件。我希望在用户单击四个选项后弹出警报。我在输出中添加了计数器变量“ turns”,因此我可以查看它是否已正确计数,但事实并非如此。
var question1 = new Array();
var turns = 0;
window.onload = function () {
var eSelect = document.getElementById('question1');
var optOtherReason = document.getElementById('displayresponse');
var options = document.getElementsByTagName("option");
eSelect.onchange = function () {
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 + turns;
turns + 1;
}
if (eSelect.selectedIndex == 3) {
optOtherReason.style.display = 'block';
turns - 1;
}
if (turns = 4) {
alert("hey your turn is over")
}
}
<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="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 ( turns == 4)
另外,它turns
是一个变量,因此要对一个变量求和/减去:
turns += 1
turns -= 1
或者,如评论中指出的,您也可以使用:
turns++;
turns--;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句