我创建的If语句忽略了我放在其中的else选项。前三个选项有效但如果我输入类似(y,x,z)的东西,它会给我两个随机的if语句,同时仍然给我#34;抱歉"信息。经过大量的研究后,我仍然无法弄清楚我做错了什么。
var questionB = new Array ();
var key1= ["x", "y", "z"];
var key2= ["y","z","x"];
var key3 =["z", "x", "y"];
window.onload = function() {
var eSelect = document.getElementById('question1');
var optOtherReason = document.getElementById('displayresponse');
var options = document.getElementsByTagName("option");
eSelect.onchange = function() {
questionB.push(eSelect.value);
var y= document.getElementById("answerTest");
y.innerHTML= questionB;
if (eSelect.selectedIndex ==0) {
optOtherReason.style.display = 'block';
}
if (questionB.length>2) {
document.getElementById('question1').style.visibility="hidden";
document.getElementById('generateButton').style.display = "block";
}
}
}
generate= function () {
for ( var i=0; i<1; i++)
{
if (questionB[i]==key1[i]) {
alert("your drink is an orange vanilla protein shake")
document.getElementById("result1").style.display= "block";
}
else if (questionB[i]==key2[i]) {
alert("your drink is a strawberry nut protein shake")
document.getElementById("result2").style.display= "block";
}
else if(questionB[i]==key3[i]){
alert("your drink is a wild berry protein shake")
document.getElementById("result3").style.display= "block";
}
else {document.getElementById("result4").style.display= "block";}
document.getElementById("result5").style.display= "block";
}//end of generate function
}
function reloadPage(){
window.location.reload();
}
<select id="question1" name="question" style="display:block;">
<option value=""></option>
<option value="x">Reason1</option>
<option value="y">Reason2</option>
<option value="z">Otherreason</option>
<!--<option value="none">None</option>-->
你的结果是草莓坚果奶昔。点击这里查看原料 。
你的结果是狂野的浆果。点击这里查看原料 。
对不起,请再试一次。
请再玩一次。
答案 0 :(得分:1)
否则ifs不太好用。请尝试使用开关(这使得代码更加清晰和可读):
switch(questionB[i])
case(key1[i]) {
alert("your drink is an orange vanilla protein shake")
document.getElementById("result1").style.display= "block";
}
case(key2[i]) {
alert("your drink is a strawberry nut protein shake")
document.getElementById("result2").style.display= "block";
}
case(key3[i]){
alert("your drink is a wild berry protein shake")
document.getElementById("result3").style.display= "block";
}
default{document.getElementById("result4").style.display= "block";}
document.getElementById("result5").style.display= "block";
}//end of generate function
请检查语法...不太确定是否正确。
答案 1 :(得分:0)
如果你在多个if语句中有相同的操作数,那么尝试使用switch而不是嵌套的if else语句,这将是更好的方法;像这里 questionB [i]
generate = function(){
for( var i=0; i<1; i++){
switch(questionB[i]){
case key1[i]:
alert("your drink is an orange vanilla protein shake");
document.getElementById("result1").style.display= "block";
break;
case key2[i]:
alert("your drink is a strawberry nut protein shake");
document.getElementById("result2").style.display= "block";
break;
case key3[i]:
alert("your drink is a wild berry protein shake");
document.getElementById("result3").style.display= "block";
default:
document.getElementById("result4").style.display= "block";
}
document.getElementById("result5").style.display= "block";
}
}