Jquery:return:false提交不工作 - 表单仍然提交

时间:2013-08-20 12:45:04

标签: jquery form-submit

表单是否还在提交?验证检查有效但如果一切输入正确,表单提交并且ajax请求不会触发。

$("#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;
});

1 个答案:

答案 0 :(得分:2)

这应该有效

$("#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();
});