javascript没有将用户带到重定向页面

时间:2017-09-14 02:33:37

标签: javascript html css

嗨,所以即时尝试构建用户重新定向到新页面的内容,所以这是我的代码

var blue = ["question1button1", "question1button2"];
var secondquestion = ["question2button1", "question2button2"];
var button1, button2, button3, button4 = false;

function myFunction() {
    var favorite = blue[Math.floor(Math.random() * blue.length)];
    var postmessage = favorite;
    document.getElementById("myBtn").value;
    document.getElementById("demo").innerHTML = postmessage;

    if (postmessage == "test"){
    	button1 = true;
    	alert("test");
    }
    if (postmessage == "test1"){
        button2 = true;
    	alert("test1");
    }
    if (postmessage == "test2"){
        button3 = true;
    	alert("test2");
    }

    if (postmessage == "test3"){
        button4 = true;
    	alert("test3");
    }
}

function next(){
    if(button1 == true || button2 == true){
        window.location = "test/test1.html";
    }
    else{
        alert("choose a side");
    }
}
<button id="myBtn" value="myvalue" onclick="myFunction()">Try it</button>

<p id="demo"></p>
<button id="next1" value="gonext" onclick="next()">next</button>

所以我的问题是我按了一个按钮,但它仍然告诉我其他声明

1 个答案:

答案 0 :(得分:0)

这是因为你在邮寄后的情况有误。将postmessage == "test"改为postmessage == "question1button2"postmessage == "question1button1"

试试这段代码..

var blue = ["question1button1", "question1button2"];
var secondquestion = ["question2button1", "question2button2"];
var button1, button2, button3, button4 = false;

function myFunction() {
  var favorite = blue[Math.floor(Math.random() * blue.length)];
  var postmessage = favorite;
  document.getElementById("myBtn").value;
  document.getElementById("demo").innerHTML = postmessage;

  if (postmessage == "question1button1"){
    button1 = true;
    alert("test");
  }
  if (postmessage == "question1button2"){
    button2 = true;
    alert("test1");
  }
  if (postmessage == "test2"){
    button3 = true;
    alert("test2");
  }
  if (postmessage == "test3"){
    button4 = true;
    alert("test3");
  }	
}

function next(){
  if(button1 == true || button2 == true){
    window.location = "test/test1.html";
  }
  else{
    alert("choose a side");
  }
}
<button id="myBtn" value="myvalue" onclick="myFunction()">Try it</button>

<p id="demo"></p>
<button id="next1" value="gonext" onclick="next()">next</button>