The If statement I created ignores the else option I place in it. The first three options work but if i put in something like (y,x,z), it will give me two random if statements, while still giving me the "sorry" message. After doing a lot of research I still cant figure out what I did wrong.
我创建的If语句忽略了我放在其中的else选项。前三个选项有效但如果我输入类似(y,x,z)的东西,它会给我两个随机的if语句,同时仍然给我“抱歉”的消息。经过大量的研究后,我仍然无法弄清楚我做错了什么。
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>-->
2 个解决方案
#1
1
Else ifs are not too good to use. Try the switch instead(This makes the code much more cleaner and readable as well):
否则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
Please check the syntax...not too sure if that is correct or not.
请检查语法...不太确定是否正确。
#2
0
if u have same operand in multiple if statements then try to use switch instead of nested if else statement that would be better approach; like here questionB[i]
如果你在多个if语句中有相同的操作数,那么尝试使用switch而不是嵌套if else语句,这将是更好的方法;像这里问题B [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";
}
}
#1
1
Else ifs are not too good to use. Try the switch instead(This makes the code much more cleaner and readable as well):
否则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
Please check the syntax...not too sure if that is correct or not.
请检查语法...不太确定是否正确。
#2
0
if u have same operand in multiple if statements then try to use switch instead of nested if else statement that would be better approach; like here questionB[i]
如果你在多个if语句中有相同的操作数,那么尝试使用switch而不是嵌套if else语句,这将是更好的方法;像这里问题B [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";
}
}