jQuery:返回:提交时返回false无效-表单仍提交 [英] Jquery: return: false on submit not working - form still submits

查看:109
本文介绍了jQuery:返回:提交时返回false无效-表单仍提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格仍在提交是有原因的吗?验证检查工作正常,但是如果一切输入正确,表单将提交并且ajax请求不会触发.

Is there a reason that the form is still submitting? The validation checks work but if it everything is input correctly, the form submits and the ajax request does not fire.

$("#formRegister").submit(function () {
// Start problem
    var mode = $("registerMode").val();
// End problem
    var username = $("#registerUsername").val();
    var password = $("#registerPassword").val();
    var passwordConfirm = $("#registerPasswordConfirm").val();
    var avatar = $("#registerAvatar").val();

    if (username == 'Username') {
        $("#registerError").html('You must enter a username');
    } else if (password == 'Password') {
        $("#registerError").html('You must enter a password');
    } else if (password != passwordConfirm) {
        $("#registerError").html('Your passwords did not match');
    } else if (avatar == 'Avatar URL') {
        $("#registerError").html('You must enter the URL for your combine avatar');
    } else {
        $.ajax({
            type: "POST",
            url: "processUsers.php",
            data: {
                mode: mode,
                username: username,
                password: password,
                avatar: avatar
            },
            dataType: "JSON",
            success: function(data) {
                alert('success!');
            }
        });
    }

    return false;
});

推荐答案

这应该有效

$("#formRegister").submit(function (event) {
var username = $("#registerUsername").val();
var password = $("#registerPassword").val();
var passwordConfirm = $("#registerPasswordConfirm").val();
var avatar = $("#registerAvatar").val();

if (username == 'Username') {
    $("#registerError").html('You must enter a username');
} else if (password == 'Password') {
    $("#registerError").html('You must enter a password');
} else if (password != passwordConfirm) {
    $("#registerError").html('Your passwords did not match');
} else if (avatar == 'Avatar URL') {
    $("#registerError").html('You must enter the URL for your combine avatar');
} else {
    $.ajax({
        type: "POST",
        url: "processUsers.php",
        data: {
            mode: mode,
            username: username,
            password: password,
            avatar: avatar
        },
        dataType: "JSON",
        success: function(data) {
            alert('success!');
        }
    });
}
event.preventDefault();
});

这篇关于jQuery:返回:提交时返回false无效-表单仍提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆