Html Javascript根据输入重定向表单提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17007010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:15:29  来源:igfitidea点击:

Javascript redirect on form submit depending on input

javascripthtmlforms

提问by tpitman

I'm working on a simple javascript quiz, and I can't for the life of me get Javascript to submit the form and open the result in the same page, regardless of whether I use location.href, open.window, or whether I set "_self" as the target. Doesn't seem to matter what I do...

我正在做一个简单的 javascript 测验,我一生都无法让 Javascript 提交表单并在同一页面中打开结果,无论我是使用 location.href、open.window 还是我将“_self”设置为目标。我做什么似乎并不重要...

function answer() {
  var response = document.getElementById('answer').value;
  if (response == "correctanswer")
    location.href('right.html');
  else
    location.href('wrong.html');
}
<form onSubmit="answer()" id="answer" target="_self">
  <input type="text" maxlength="55" class="box" autofocus />
  <input type="submit" class="submit" value="SUBMIT" />
</form>

So, what I want to happen is, when the user submits the form, they go to "right.html" if they typed correctanswerinto the text box, or "wrong.html" if they typed anything else.

所以,我想发生什么,当用户提交表单,他们去“right.html”如果他们输入correctanswer到文本框中,或“wrong.html”如果他们输入任何东西。

I've got it running fine, except for the fact that no matter what I do I can't get the resulting page to open in _self, but rather in another window. Every time.

我让它运行良好,除了无论我做什么我都无法打开结果页面_self,而是在另一个窗口中打开。每次。

Been driving me crazy all night.

整晚都让我发疯。

回答by T.J. Crowder

Five things:

五件事:

  1. Remove the targetattribute of formentirely. The default is the same window. Although it doesn't really matter, because:

  2. Cancel the submitevent by returning falsein your onSubmit, since you're handling the form in your own code. One easy way to do this is have your function return false, and in the onSubmit, return the result of the call.

  3. This line is incorrect:

    var response = document.getElementById('answer').value;
    

    formelements don't have a value. You'd want to put the idon the input type="text"element instead.

  4. The hrefon locationis not a function, it's a property. You just assign to it (or just assign directly to location).

  5. This one's a bit subtle: Because you have a global function called answer, and you have an element with an id"answer", there's a conflict: Both want to end up as properties of the windowobject (one of many reasons not to use old DOM0 onxyzattributes — or global functions, come to that). So you need to change the name of one of them, e.g., change the function to checkAnsweror similar.

  1. 完全去除的target属性form。默认是同一个窗口。虽然这并不重要,因为:

  2. submit通过false在您的 中返回来取消事件onSubmit,因为您是在自己的代码中处理表单。一种简单的方法是让您的函数 return false,并在onSubmit, 中返回调用的结果。

  3. 这一行是不正确的:

    var response = document.getElementById('answer').value;
    

    form元素没有value. 你会希望把idinput type="text"元素来代替。

  4. hreflocation是不是一个函数,这是一个性质。您只需分配给它(或直接分配给location)。

  5. 这个有点微妙:因为您有一个名为 的全局函数answer,并且您有一个带有 的元素id"answer",所以存在冲突:两者都希望最终成为window对象的属性(不使用旧的 DOM0onxyz属性的众多原因之一- 或全局功能,来吧)。因此您需要更改其中之一的名称,例如,将功能更改为checkAnswer或类似。

So:

所以:

<form onSubmit="return checkAnswer();">
<input id="answer" type="text" maxlength="55" class="box" autofocus />
<input type="submit" class="submit" value="SUBMIT" />
</form>

And:

和:

function checkAnswer(){
    var response = document.getElementById('answer').value;
    if (response == "correctanswer")
        location = 'right.html';
    else
        location = 'wrong.html';
    return false;
}

Full Example: Live Copy| Live Source

完整示例:实时复制| 直播源

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <form onSubmit="return checkAnswer();">
  <input id="answer" type="text" maxlength="55" class="box" autofocus />
  <input type="submit" class="submit" value="SUBMIT" />
  </form>
  <script>
  function checkAnswer(){
      var response = document.getElementById('answer').value;
      if (response == "correctanswer")
          location = 'http://jsbin.com/ukifoh/1'; // 'right.html';
      else
          location = 'http://jsbin.com/ukifoh/2'; // 'wrong.html';
      return false;
  }
  </script>
</body>
</html>


I would recommend always using consistent, useful code indentation, and I'm a big fan of always using blocks, not just statements, with control structures.

我建议始终使用一致的、有用的代码缩进,并且我非常喜欢始终使用块,而不仅仅是语句和控制结构。