1

我是 javascript 的新手,我目前被困在这个问题上。我需要根据某些移动的数量重定向到另一个页面。例如:如果找到所有对后的移动次数在 4 - 8 之间,您将被重定向到第 1 页查看结果,10 - 16 在第 2 页等等,但我关于结果的代码仍然转到第 1 页。这是我的 js 代码:

 function validate() {
  var clickHere = document.getElementById("terms");
  if(clickHere.checked){
    if(counterVal >= 4 || counterVal <=8){
      location.replace("https://www.w3schools.com/js/")
    }
    else{
      location.replace("https://javascript.info/")
    }
  }else{
    
    alertify.error('Click the checkbox first.')
  }

我一直在尝试不同的方法来解决这个问题,但没有运气。感谢您的帮助。

4

3 回答 3

1

使用 JavaScript 重定向到 URL 的最简单方法是使用window.location.href. JavaScript 代码如下所示: window.location.href = ‘https://ExampleURL.com/’; 它是一个属性,告诉您当前正在查看哪个 URL。设置一个新值,您是在告诉浏览器加载该新 URL,类似于用户单击链接时会发生的情况。您的功能应如下所示:

function validate() {
  var clickHere = document.getElementById("terms");
  if(clickHere.checked){
    if(counterVal >= 4 || counterVal <=8){
      window.location.href = "https://www.w3schools.com/js/"
    }
    else{
      window.location.href ="https://javascript.info/"
    }
  }else{
    
    alertify.error('Click the checkbox first.')
  }
于 2021-12-22T06:19:31.610 回答
1

从你的问题中,你提到了for example: if the number of moves after you find all the pairs are between 4 - 8, you will be redirected to page 1 to see your result, 10 - 16 is on page 2 and so on, but my code regarding of result still goes to page 1

正如您提到的countVal,介于 4 到 8 之间,那么它将用于此链接https://www.w3schools.com/js/

但是在此,如果您提到的条件为counterVal <=4. 如果 countVal 大于 8,则条件为真,这就是问题所在。

if(counterVal >= 4 || counterVal <=8){

你需要像这样重写它

if(counterVal >= 4 && counterVal <=8){

我希望这会很有用。

于 2021-12-22T06:33:52.883 回答
0

代码必须是

if(counterVal >= 4 && counterVal <=8){
   window.location.href = "https://www.w3schools.com/js/1"
}else if(counterVal >= 10 && counterVal <=16){
   window.location.href = "https://www.w3schools.com/js/2"
}else{
  location.replace("https://javascript.info/")
}
于 2021-12-22T07:05:23.893 回答