我在这里哪里做错了?
我在一个页面上有多个相同的表单。我有这个代码,除了天数计算外,它只适用于第一种形式:
var options = {
url: 'http://www.mysite.com/wp-admin/admin-ajax.php',
//target:'html', // target element(s) to be updated with server response
type: 'POST',
success: function(e) {
$(this).parent().parent('.modal.in').modal('hide');
$('.formmodal').modal('hide');
$('#thanks').modal('show');
return false;
},
clearForm: true,
error: function(e) {
$('#nope').modal('show');
return false;
},
resetForm: true,
data: {
action: 'submit_package_form'
}
};
function calcDates() {
$('.pkdate').on('change', function(e) {
var dstart = new Date($('#arrive').datepicker('getDate'));
var dend = new Date($('#leave').datepicker('getDate'));
var diff = 0;
if (dstart && dend) {
diff = Math.floor((dend.getTime() - dstart.getTime()) / 86400000); // ms per day
if(diff > 0){
$(this).parent().parent().find('#nights').val(diff);
}
}
});
}
$(".form").each(function() {
var validator = $(this).validate({
onsubmit: true,
errorClass: "alert-error",
validClass: "success",
rules: {
name: "required",
email: {
email:true,
required:true
},
address: "required",
postcode: "required",
city: "required",
telephone: "required",
arrive: "required",
leave: "required",
nights: "required",
travelers: "required",
singles: "required",
doubles: "required",
fee: "required"
},
submitHandler: function(form) {
$(form).ajaxSubmit(options);
}
});
$('.rensa').click(function() {
validator.resetForm();
$('.extrab, .extrat').remove();
$('label.alert-error').hide();
$('.alert-error').removeClass("alert-error");
$('.alert-error').css('display','none');
$('.alert-error').remove();
});
calcDates(this);
});
我需要这个适用于页面上的所有表单。我已经尝试在 each 中移动 calcDates 函数,但这并没有什么不同。