0

又一次,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;
}

请帮忙!我开始扯头发了!

4

2 回答 2

2

看起来 IE 无法使用使用的日期格式,您可以使用$.datepicker.parseDate来解析具有自定义格式的日期

checkTheDate = $.datepicker.parseDate( 'd/mm/yy', date.data('name'));
于 2013-10-24T01:07:03.940 回答
0

我不明白为什么复选框不在表单中。

无论如何,要将“2013 年 1 月 11 日”转换为日期(假设 d/m/y),请使用如下函数:

function toDate(s) {
  var bits = s.split(/\D/);
  return new Date(bits[2], --bits[1], bits[0]);
}

注意日期字符串的解析很大程度上取决于实现,并且通常需要“m/d/y”顺序。手动解析是迄今为止最好的方法。

于 2013-10-24T01:15:42.900 回答