preventDefault()无法处理表单请求

时间:2018-05-04 13:48:43

标签: javascript jquery ajax forms

我有以下表单处理用户请求的密码恢复脚本。

<form class="forget-form" id="forget-form" action="datacenter/functions/passwordRecovery.php" method="POST">
    <h3 class="font-green">Esqueceu sua senha?</h3>
    <p> Digite seu e-mail abaixo para receber o procedimento para uma nova senha.</p>
    <div class="form-group" style="margin-bottom:0px">
        <input class="form-control placeholder-no-fix" type="email" autocomplete="off" placeholder="Seu e-mail" name="email" id="email" required/>
    </div>

    <div class="form-actions">
        <button type="button" id="back-btn" class="btn red btn-outline uppercase"><i class="fa fa-chevron-circle-left"></i> voltar</button>
        <button type="submit" class="btn btn-success uppercase pull-right uppercase"><i class="fa fa-paper-plane"></i> enviar</button>
    </div>
</form>

为此,我使用ajax将用户保持在同一屏幕上,没有任何重定向。

$("#forget-form").submit(function(e){
    e.preventDefault();
    var email = $("#email").val();
    if(isEmail(email) == true){
        $.ajax({
            url: 'datacenter/functions/passwordRecovery.php',
            type: 'POST',
            data: new FormData(this),
            async: true,
            contentType: false,
            processData: false,
            success: function(data){
                console.log(data);
            },
            error: function(error, errorThrown, errorShowcase){
                console.log(data);
            }
        });
    } else {
        console.log('this is not an email');
    }
});

文件passwordRecovery.phpecho "success";登录控制台,只是为了检查它是否真的有效,并且它正常工作,但速度太快,它会在控制台上登录,然后重定向到passwordRecovery.php,不会阻止表单的默认操作。

你有没有遇到过这种情况?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

$('#forget-form').submit(function () {
//your code
return false;
});