一起使用 jquery validate 和 $.ajax 的正确方法是什么?一旦表单通过验证,我将尝试通过 Ajax 保存数据:
jQuery(document).ready(function() {
$('#np-submit').click(function() {
if($("#newsletter-subscription").validate()) {
var name = $("input#fullname").val();
var email = $("input#email").val();
var interest = $("select#interest").val();
var dataString = 'name=' + name + '&email=' + email + '&interest=' + interest;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "../save_address.php",
data: dataString,
success: function() {
alert("Contact Form Submitted!");
}
});
});
$("#newsletter-subscription").validate({
rules: {
fullname: "required",
email: {
required: true,
email: true,
},
interest: "required"
},
messages: {
fullname: "Please enter a your first name.",
email: "Please enter a valid email address.",
interest: "Please select your area of interest."
}
});
});
});