是否有理由表格仍在提交?验证检查工作但如果一切都输入正确,表单提交并且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(); });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。