2

我有以下代码来设置 datepicker 的最小日期。

$( "#sd" ).datepicker({
    // before datepicker opens run this function
    beforeShow: function(){
        // this gets today's date       
        var theDate = new Date();
        // sets "theDate" 2 days ahead of today
        theDate.setDate(theDate.getDate() + 2);
        // set min date as 2 days from today
        $(this).datepicker('option','minDate',theDate);         
    },
    // When datepicker for start date closes run this function
    onClose: function(){
        // this gets the selected start date        
        var theDate = new Date($(this).datepicker('getDate'));
        // this sets "theDate" 1 day forward of start date
        theDate.setDate(theDate.getDate() + 1);
        // set min date for the end date as one day after start date
        $('#ed').datepicker('option','minDate',theDate);

    }
});

如果今天的日期是 11/14/2012,那么 datepicker 的值就是 11/16/2012。但是,由于日期应该基于服务器的日期时间,所以我不能使用 javascript 日期,因为它将获取计算机的当前日期而不是服务器的日期。

所以我所做的就是将其更改为以下代码:

$( "#sd" ).datepicker({
    // before datepicker opens run this function
    beforeShow: function(){
        // this gets today's date       
        var theDate = new Date();
        // sets "theDate" 2 days ahead of today
        //theDate.setDate(theDate.getDate() + 2);
        theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);
        // set min date as 2 days from today
        $(this).datepicker('option','minDate',theDate);         
    },
    // When datepicker for start date closes run this function
    onClose: function(){
        // this gets the selected start date        
        var theDate = new Date($(this).datepicker('getDate'));
        // this sets "theDate" 1 day forward of start date
        theDate.setDate(theDate.getDate() + 1);
        // set min date for the end date as one day after start date
        $('#ed').datepicker('option','minDate',theDate);

    }
});

请看一下这一行:

theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);

顺便说一句,我正在使用 codeigniter 会话。

这很好用,但问题是 datepicker 中的最小日期未设置为 2012 年 11 月 16 日,而是设置为 2012 年 11 月 2 日。

任何人都知道如何将 PHP 值正确插入到 jQuery 中?

4

1 回答 1

0

我已经找到了答案。这里是:

$( "#sd" ).datepicker({
    // before datepicker opens run this function
    beforeShow: function(){
        // this gets today's date       
        var theDate = new Date();
        theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>);
        // set min date as 2 days from today
        $(this).datepicker('option','minDate',theDate);         
    },
    // When datepicker for start date closes run this function
    onClose: function(){
        // this gets the selected start date        
        var theDate = new Date($(this).datepicker('getDate'));
        // this sets "theDate" 1 day forward of start date
        theDate.setDate(theDate.getDate() + 1);
        // set min date for the end date as one day after start date
        $('#ed').datepicker('option','minDate',theDate);

    }
});

它应该只提取日期而不是整个日期。

theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>);

代替

theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);
于 2012-11-14T04:32:35.730 回答