联系表格提交两次
问题描述:
我增加了邮件订阅弹出形式,它是在提交发送两次,也正在显示的确认消息的两倍联系表格提交两次
下面是代码:
$(document).ready(function() {
$(".modalbox").fancybox();
$("#contact").submit(function(e) {
e.preventDefault();
var emailval = $("#email").val();
var mailvalid = validateEmail(emailval);
if(mailvalid == false) {
$("#email").addClass("error");
}
else if(mailvalid == true){
$("#email").removeClass("error");
}
if(mailvalid == true) {
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$("#send").replaceWith("<em>sending...</em>");
$.ajax({
type: 'POST',
url: 'sendmessage.php',
data: $("#contact").serialize(),
success: function(data) {
if(data == "true") {
$("#contact").fadeOut("fast", function(){
$(this).before("<p><strong>Success! You have signed up for a trial. A member of our team wil soon be in contact :)</strong></p>");
setTimeout("$.fancybox.close()", 1700);
});
}
}
});
}
});
});
答
我不“吨认为有任何必要写这行:
$("#contact").submit(function() { return false; }); // Remove this
您可以删除此行:
$("#send").on("click", function(){ // Remove this
而且写来代替:
$("#contact").submit(function(e) { // Add this
e.preventDefault(); // Add this
var emailval = $("#email").val();
var mailvalid = validateEmail(emailval);
/* Other code */
});
非常感谢您! 它一开始并没有工作,我以前在网上浏览,看到一些人把e.stopImmediatePropagation();在e.preventDefault()下;这对我有用。 再次感谢:) – Ashton
请务必投票并接受答案,如果它解决您的问题。 :) –