1
我創建了一個表單,在提交時不需要頁面刷新幷包含驗證。我的問題是我必須點擊提交按鈕兩次發送表單。我猜這個問題有關:需要點擊兩次表單提交(AJAX)
submitHandler: function(form) {
$('.mainform').submit(function(){
我的問題是我只是不能找出更好的方法來做到這一點。我已經包括下面的完整JS:
$(document).ready(function() {
$('<i class="fa-check"></i><i class="fa-exclamation"></i>').appendTo($('.mainform section'));
$(".mainform").validate({
// Rules for form validation
rules: {
firstname: {
required: true
},
lastname: {
required: true
},
email: {
required: true,
email: true
},
phone: {
required: true,
minlength: 10
},
services: {
required: true
},
message: {
required: true,
minlength: 10
},
human: {
required: true,
range: [4, 4]
}
},
// Messages for form validation
messages: {
firstname: {
required: 'Please enter your first name'
},
lastname: {
required: 'Please enter your last name'
},
email: {
required: 'Please enter your email address',
email: 'Please enter a VALID email address'
},
phone: {
required: 'Please enter your phone number'
},
services: {
required: 'Please enter the services you are looking for'
},
message: {
required: 'Please enter your message'
},
human: {
required: 'Please answer security question',
range: 'That is incorrect'
}
},
// Do not change code below
errorPlacement: function(error, element) {
error.appendTo(element.parent());
},
submitHandler: function(form) {
$('.mainform').submit(function(){
$.ajax({
url: 'toAction.php',
data: $(this).serialize(),
type: 'POST',
success: function(data) {
console.log(data);
$("#success").show().fadeOut(15000); //=== Show Success Message==
},
error: function(data) {
$("#error").show().fadeOut(15000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});
}
});
});
返回
false
刪除這個事情$(「MainForm的。」)提交(函數(){})。你不需要再包裝這個東西了。只需使用$ .ajax ... – jewelnguyen8 2014-10-09 04:08:09嗯,附加提交處理程序的提交處理程序! – 2014-10-09 04:13:37
是的......我有一些學習要做。多謝你們。 – 2014-10-09 18:47:19