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

前端之家收集整理的这篇文章主要介绍了Jquery:return:false提交不工作 – 表单仍然提交前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有理由表格仍在提交?验证检查工作但如果一切都输入正确,表单提交并且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;
});

解决方法

这应该工作
$("#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",data: {
            mode: mode,avatar: avatar
        },success: function(data) {
            alert('success!');
        }
    });
}
event.preventDefault();
});

猜你在找的jQuery相关文章