作为javascript的新手,我在编写If语句时遇到问题,事件发生在第四轮之后。我想在用户点击四个选项后弹出警报。我将计数器变量“turn”添加到输出中,这样我就可以看到它是否已经正确计数但是没有。
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>
答案 0 :(得分:4)
要比较两个表达式,您需要使用==
:
if ( turns == 4)
此外,turns
是一个变量,所以要求和/减去你应该使用的变量:
turns += 1
turns -= 1
或者,正如评论中指出的那样,您也可以使用:
turns++;
turns--;