重定向具有多个提交输入的表单

Redirecting a form with multiple submit inputs

本文关键字:输入 表单 提交 重定向      更新时间:2023-09-26

我有一个表单,它有3类型为"submit"的单独输入。

前两个输入在被点击后会输出一条消息,但最后一个输入应该重定向到另一个页面,但它没有这样做我认为这是因为表单在到达JavaScript进行重定向之前正在提交并刷新页面

这是我的代码:

FORM.php

<form class="form" id="form" method="POST" enctype="application/x-www-form-urlencoded">
    <br>
         <input type="email" name="email" id="email" maxlength="80" value="<?php echo $email ?>" placeholder="Enter Your Email" /><br /><br>
        <input id="button4" type="submit" value="Get Security Question" name="submit2" style="cursor:pointer;"/><br> 
            <br><input type="password" name="securitya"id="securitya" maxlength="20" value="<?php echo $securitya ?>" placeholder="Enter Your Security Answer" /> <br />
        <br>
    <input id="button3"type="submit" value="Check Answer" name="submit" style="cursor:pointer;"/>
    <br>
        <br><input type="password" name="newpassword" id="newpassword" maxlength="20" placeholder="Enter Your New Password" /> <br />
    <br><input type="password" name="confirmpassword" id="confirmpassword" maxlength="20" placeholder="Re-Enter Your New Password" /> <br />
<br>
    <input id="button2" type="submit" value="Change Password" disabled="disabled" name="submit" style="cursor:pointer;"/>

JavaScript

jQuery(function(){
        $("#button2").click(function(){                                         
        $(".error").hide();
        var hasError = false;
        var passwordVal = $("#newpassword").val();
        var checkVal = $("#confirmpassword").val();
        if (passwordVal == '') {
            $("#newpassword").after('<span class="error" >Please enter a password.</span>');
            hasError = true;
        } else if (checkVal == '') {
            $("#confirmpassword").after('<span class="error">Please re-enter your password.</span>');
            hasError = true;
        } else if (passwordVal != checkVal ) {   
            $("#confirmpassword").after('<span id = "pass" class="error">Passwords do not match.</span>');
            hasError = true;
        }
        if(hasError == true) { return false; }
        else {
            $("#button2").after('<span class="error">Passwords accepted.</span>');
            window.location.href='adminsignin.php';
            }
    }); 
});

奇怪的是,屏幕上会出现"接受密码"的消息,但没有重定向到"adminsignin.php"

有人能帮忙吗?

在javascript中,将事件传递到click处理程序,然后preventDefault()。

$("#butotn2").click(function(e) {
    e.preventDefault();
   ...
}

"提交"按钮具有默认行为,因此您需要阻止其默认行为,以便执行操作,然后使用窗口重定向。location.href.

请尝试更改为document.location = 'your URL';