简体   繁体   English

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

[英]Javascript redirect on form submit depending on input

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. 我正在进行一个简单的javascript测验,我无法为我的生活获取Javascript提交表单并在同一页面打开结果,无论我是否使用location.href,open.window,或者是否我将“_self”设为目标。 Doesn't seem to matter what I do... 我做什么似乎并不重要......

 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 correctanswer into 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. 我已经把它运行得很好,除了这样的事实,无论我做什么,我都无法在_self打开生成的页面,而是在另一个窗口中打开。 Every time. 每次。

Been driving me crazy all night. 整晚让我疯狂。

Five things: 五件事:

  1. Remove the target attribute of form entirely. 完全删除formtarget属性。 The default is the same window. 默认值是相同的窗口。 Although it doesn't really matter, because: 虽然它并不重要,因为:

  2. Cancel the submit event by returning false in your onSubmit , since you're handling the form in your own code. 通过在onSubmit返回false来取消submit事件,因为您在自己的代码中处理表单。 One easy way to do this is have your function return false , and in the onSubmit , return the result of the call. 一种简单的方法是让函数返回false ,并在onSubmit中返回调用的结果。

  3. This line is incorrect: 这行不正确:

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

    form elements don't have a value . form元素没有value You'd want to put the id on the input type="text" element instead. 您希望将id放在input type="text"元素上。

  4. The href on location is not a function, it's a property. location上的href不是函数,它是属性。 You just assign to it (or just assign directly to location ). 您只需分配给它(或直接分配给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 window object (one of many reasons not to use old DOM0 onxyz attributes — or global functions, come to that). 这个有点微妙:因为你有一个名为answer的全局函数,并且你有一个id "answer"的元素,所以存在冲突:两者都希望最终成为window对象的属性(不使用的众多原因之一)旧的DOM0 onxyz属性 - 或全局函数,来到那里)。 So you need to change the name of one of them, eg, change the function to checkAnswer or similar. 因此,您需要更改其中一个的名称,例如,将功能更改为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. 我建议总是使用一致的,有用的代码缩进,并且我总是喜欢使用块,而不仅仅是语句,带有控制结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM