是否有理由表格仍在提交?验证检查工作但如果一切都输入正确,表单提交并且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(); });