又一次,IE相关的问题。这个想法是:在每个 td 中,复选框都有在 data-name 属性中分配给它们的日期。提交表单后,我正在尝试检查他们检查的日期是否连续,如果它们是连续的,请使用开始和结束日期更新隐藏字段,以便在下一页进行更多计算,例如价格、特价等。这在 Chrome 和 firefox 等真实浏览器中运行良好。但是 IE(和我怀疑的 safari)有问题。做一些警报,我看到日期是 NaN。这是一个片段:
的HTML:
<td><input data-name="30/10/2013" type="checkbox"></td>
<td><input data-name="31/10/2013" type="checkbox"></td>
<td><input data-name="1/11/2013" type="checkbox"></td>
<form id="booking0" onsubmit="return bookingValidation(0)" class="bookings" method="post" action="/anotherpage.asp">
<input type="hidden" name="bookStart" value="">
<input type="hidden" name="bookEnd" value="">
<input class="book" type="submit" name="Submit" value="Next">
</form>
jQuery:
function bookingValidation(formIdentifier){
var start = 0;
var checkTheDate;
var currentDate;
var diff;
var days;
var validity = false;
var checkCounter = 0
$("tr#row"+formIdentifier).find('td').each(function (){
var date = $(this).find('input');
if(!date.attr('data-name') == null || !date.attr('data-name') == "") { //if it exists, check to see if it is checked.
if( date.prop('checked') ){ //If checkbox is checked
checkCounter++; // if current checkbox is checked, increment.
if(start == 0){ //if start == 0, then this is the first date. We want to pass the date value into the forms hidden field
checkTheDate = new Date(date.attr('data-name')); //this is first check box that is checked. grab the associated date and save it
$('#booking'+formIdentifier).find('input[name=bookStart]').val(jsToVbDate(checkTheDate)); //set both start and end date, incase customer wants to do a 1 day trip
$('#booking'+formIdentifier).find('input[name=bookEnd]').val(jsToVbDate(checkTheDate));
start++; //increment so this if statement will never become true
validity = false;
}
else{ //
checkTheDate.setDate( checkTheDate.getDate() + 1); //increment check date to hopefully match current date
currentDate = new Date(date.attr('data-name')); //grab current date
diff = new Date(currentDate - checkTheDate); //subtract current date from check date, will return in milliseconds
days = diff/1000/60/60/24; //convert milliseconds into days
if(days <= 0){ //if days is less than or equal to 0, this means that the check date and current date are the same, meaning the checkboxes are consecutive
$('#booking'+formIdentifier).find('input[name=bookEnd]').val(jsToVbDate(currentDate)); //update end date with current date
validity = true;//passes validation, can continue onto booking
}
else{
alert("Checks must be consecutive");
validity = false; //fails validation, can not continue to booking
return false;
}
}
} //2nd if
} //1st if
}); //end .each() loop
//checks how many checkboxes were checked. If 1 or less were checked, validation failed.
if (checkCounter <= 1){
alert("You must select at a minimum of 2 days");
validity = false;
}
return validity;
}
function jsToVbDate(dateToFormat){
var bookStartY = dateToFormat.getFullYear();
var bookStartM = dateToFormat.getMonth()+1;
var bookStartD = dateToFormat.getDate();
var dateToFormat = bookStartY + "-" + bookStartM + "-" + bookStartD;
return dateToFormat;
}
请帮忙!我开始扯头发了!