Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表单提交时的Javascript重定向取决于输入_Javascript_Html_Forms - Fatal编程技术网

表单提交时的Javascript重定向取决于输入

表单提交时的Javascript重定向取决于输入,javascript,html,forms,Javascript,Html,Forms,我正在做一个简单的javascript测试,我一辈子都无法让javascript提交表单并在同一页面中打开结果,不管我是使用location.href、open.window还是将“\u self”设置为目标。我做什么似乎无关紧要 函数应答(){ var response=document.getElementById('answer')。值; 如果(响应=“正确答案”) location.href('right.html'); 其他的 location.href('error.html');

我正在做一个简单的javascript测试,我一辈子都无法让javascript提交表单并在同一页面中打开结果,不管我是使用location.href、open.window还是将“\u self”设置为目标。我做什么似乎无关紧要

函数应答(){
var response=document.getElementById('answer')。值;
如果(响应=“正确答案”)
location.href('right.html');
其他的
location.href('error.html');
}

五件事:

  • 完全删除
    表单
    目标
    属性。默认设置为同一窗口。虽然这并不重要,因为:

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

  • 这行不正确:

    var response = document.getElementById('answer').value;
    
    表单
    元素没有
    。您应该将
    id
    放在
    input type=“text”
    元素上

  • 位置上的
    href
    不是函数,而是属性。您只需分配给它(或直接分配给
    位置

  • 这一个有点微妙:因为您有一个名为
    answer
    的全局函数,并且您有一个id为
    的元素
    “answer”
    ,所以存在一个冲突:两者都希望最终成为
    窗口
    对象的属性(不使用旧的DOM0
    onxyz
    属性或全局函数的原因之一就是这样)。因此,您需要更改其中一个函数的名称,例如,将函数更改为
    checkAnswer
    或类似的名称

  • 因此:

    完整示例:|

    
    JS-Bin
    函数checkAnswer(){
    var response=document.getElementById('answer')。值;
    如果(响应=“正确答案”)
    地点:http://jsbin.com/ukifoh/1“;/”right.html“;
    其他的
    地点:http://jsbin.com/ukifoh/2“;/”错了;
    返回false;
    }
    


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

    哇!非常感谢所有帮助我清除晦涩代码的人!以及快速的回复!
    <form onSubmit="return checkAnswer();">
    <input id="answer" type="text" maxlength="55" class="box" autofocus />
    <input type="submit" class="submit" value="SUBMIT" />
    </form>
    
    function checkAnswer(){
        var response = document.getElementById('answer').value;
        if (response == "correctanswer")
            location = 'right.html';
        else
            location = 'wrong.html';
        return false;
    }
    
    <!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>