0

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;
});
4

1 回答 1

2

This should work

$("#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();
});
于 2013-08-20T12:51:08.347 回答