javascript - 无法从输入字段中获取日期并将其转换为 IE8 中的日期对象

标签 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;
}

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

最佳答案

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

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

关于javascript - 无法从输入字段中获取日期并将其转换为 IE8 中的日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19555091/

相关文章:

javascript - 需要 jquery 模式弹出指导。我究竟做错了什么?

jquery - 当使用 $.ajax 以 JSON 对象作为数据时,request.getParameter() 返回 null

jquery - 菜单移动样式,但也处于桌面全高

javascript - 使用 javascript 填充页面 html div 元素

javascript - 防止鼠标松开时默认

javascript - 使输入元素不可见

html - 如何在 Chrome 中嵌入 PDF 而不出现错误或警告

javascript - href HTML 调用页面 "UP"目录

javascript - 我在 JavaScript 中遇到了未定义的问题,并且不明白为什么

javascript - 在哪里可以找到用于在客户端浏览器中裁剪图像的类似 gravatar 的脚本?