无法从输入字段中获取日期并在IE8中变为日期对象

时间:2013-10-24 01:03:47

标签: javascript jquery html date internet-explorer-8

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

请帮忙!我开始把头发撕掉了!

2 个答案:

答案 0 :(得分:2)

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

checkTheDate = $.datepicker.parseDate( 'd/mm/yy', date.data('name'));

答案 1 :(得分:0)

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

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

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

注意解析日期字符串主要取决于实现,并且通常需要“m / d / y”顺序。手动解析是目前最好的方法。