2013-08-20 149 views
0

表单仍在提交的原因是什么?验证检查工作,但如果一切都输入正确,表单提交和Ajax请求不会触发。jquery:返回:提交时不起作用 - 表单仍提交

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

可以请您给HTML太 – Puneet

+0

哪里变量被宣布为“数据”选项? – hex4

+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(); 
}); 
+0

@Guerra ...大声笑打我10秒...忘了登录。 – Epiphany

+0

@Epiphany LOL,在这上面更快=]] – Guerra

+0

'return false;'应该也能工作......它与e.preventDefault();和e.stopPropagation() – tomaroo