我有一个表单,我正在使用 jQuery 验证来验证输入是否完整。然而,正在发生的事情是验证器正在显示消息,但它也在对 boxadd.php 进行 Ajax 调用。我的印象是这个 ajax 调用是在成功验证后才进行的。为什么在输入还没有完成的情况下会到达 Ajax 事件。谢谢
jQuery代码
$(function () {
$.validator.setDefaults ({
errorClass: 'form_error',
errorElement: 'div'
});
$("#BA_boxform").validate({
rules: {
BA_customer: {
required: true
},
customerdept: {
required: true
},
customeraddress: {
required: true
},
BA_service: {
required: true
},
BA_box: {
required: true
},
BA_destdate: {
required: true
},
BA_authorised: {
required: true
}
},
messages: {
BA_customer: {
required: '<br />* required: You must select a customer'
},
customerdept: {
required: "<br />* required: You must select a department"
},
customeraddress: {
required: "<br />* required: You must select a customer address"
},
BA_service: {
required: "<br />* required: You must select a service level"
},
BA_box: {
required: "<br />* required: You must enter a box number for intake"
},
BA_destdate: {
required: "<br />* required: You must enter a destruction date"
},
BA_authorised: {
required: "<br />* required"
}
}
});
$('#BA_boxform').on('submit', function () {
var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val();
//alert(formdata);
$.ajax({
type: "POST",
url: "/domain/admin/requests/boxes/boxesadd.php",
data: formdata,
dataType: 'json',
success: function (msg) {
//alert(msg);
if (typeof msg.boxerrortext !== "undefined" && msg.boxerrortext == "You must enter a box for intake") {
$("#BA_addbox").html(msg.boxerrortext);
} else {
$("#BA_addbox").html("You have successfully added box(es) " + '<span style="font-weight: bold;color:black;">' + msg.box + '</span>' + ' to the archive.' + '<br />' + 'You may now close this window or input more boxes.');
$("#BA_boxform").get(0).reset();
}
//$("#confirm_department").hide();
/*
var $dialog = $('<div id="dialog"></div>')
.html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'Box intake submission successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
*/
//alert(msg);
//console.log(msg);
//$("#BA_addbox").html(msg.box);
//$("#formImage .col_1 li").show();
//$("#BA_boxform").get(0).reset();
//$("#boxaddform").hide();
}
});
return false;
});
});