可能重複:
How to prevent form from being submitted?防止表單被提交 - jQuery的?
這裏是我的jQuery代碼:
$(document).ready(function() {
$("#register").submit(function (event) {
var email = $('#email-agency').val();
/* EMAIL VALIDATION */
if (email == ""){
$('#email-agency').css({"border-color":"#3399ff"});
$('#email-err').html("You must enter your e-mail address.").removeClass("success_msg").addClass("error_msg");
}else{
$('#email-err').html("<img src='http://www.site.com/folder/ajax-loader.gif' /> Validating the e-mail address...").removeClass("success_msg").removeClass("error_msg");
$.get('http://www.site.com/folder/email.php',{id: email}, function(result){
if (result == "invalid format"){
$('#email-agency').css({"border-color":"#3399ff"});
$('#email-err').html(email + " is not a valid e-mail address.").removeClass("success_msg").addClass("error_msg");
event.preventDefault();
return false;
}else if (result == "taken"){
$('#email-agency').css({"border-color":"#3399ff"});
$('#email-err').html("I'm sorry, this e-mail address is already taken.").removeClass("success_msg").addClass("error_msg");
event.preventDefault();
return false;
}else if (result == "available"){
$('#email-agency').css({"border-color":"#1f6d21"});
$('#email-err').html("Congratulations, this e-mail address is available.").removeClass("error_msg").addClass("success_msg");
return true;
}
})
}
});
出於某種原因,如果電子郵件地址將(result == "taken")
- 形式仍提交。
我在做什麼錯?如果收到電子郵件地址,我怎樣才能使表格不提交?
謝謝
如何從主提交函數返回? – Latox
'return valid;'函數結尾處 – PeeHaa
爲什麼downvote? – PeeHaa