简体   繁体   English

preventDefault()对表单请求不起作用

[英]preventDefault() not working on a form request

I have the following form which process a password recovery script requested by the user. 我有以下表单,用于处理用户请求的密码恢复脚本。

<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>

For that, I'm using ajax to keep the user on the same screen without any redirect. 为此,我使用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');
    }
});

The file passwordRecovery.php only echo "success"; 文件passwordRecovery.phpecho "success"; to log on the console, just to check if it really worked, and it is working, but too fast, it logs on the console and after that redirects me to passwordRecovery.php , not preventing the default action of the form. 登录控制台,只是检查它是否确实有效,并且是否运行正常,但是速度太快,它登录到控制台,然后将我重定向到passwordRecovery.php ,而不阻止表单的默认操作。

Have you ever faced that? 你曾经面对过吗?

You can try this: 您可以尝试以下方法:

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

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

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