Jquery Datepicker和Selector

时间:2016-06-20 16:08:37

标签: jquery datepicker

我试图制作一个"动态"日期选择器,我需要做的是根据选择中的选项停用一周的某些天。类似的事情:如果我选择选项1所有星期一都被禁用,如果我选择选项2,所有星期四都被禁用,然后继续。我制作了一个有效的代码,但只有第一次,如果你改变你的选择,日期选择器只保留第一个选择。

这些是代码:

jQuery的:

if ((hrs >= 22) || (mHrs >= 24 && mHrs <= 28))
   price = 100.00
else if ((hrs >= 05) && (hrs <= 16))
   price = 20.00

HTML:

$('#comuna_select').change(function(){
    if($(this).val() == 'Cachapoal/Codegua'){
        function DisableCodegua(date) {
            var day = date.getDay();
            // If day == 1 then it is MOnday
            if (day == 0 || day == 1 || day == 2 || day== 3 || day == 5 || day == 6 ) {
                return [false] ; 
            } else { 
                return [true] ;
            }
        }

        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            defaultdate: currentDate,
            changeMonth: true,
            changeYear: true,
            yearRange: "-2:+2",
            beforeShowDay: DisableCodegua,
            minDate: '-1D',
            maxDate: '+45D',
        });
    } else if ($(this).val() == 'Cachapoal/Coinco'){
        function DisableCoinco(date) {
            var day = date.getDay();
            // If day == 1 then it is MOnday
            if (day == 0 || day == 1 || day == 2 || day== 3 || day == 4 || day == 6 ) {
                return [false] ; 
            } else { 
                return [true] ;
            }
        }

        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            defaultdate: currentDate,
            changeMonth: true,
            changeYear: true,
            yearRange: "-2:+2",
            beforeShowDay: DisableCoinco,
            minDate: '-1D',
            maxDate: '+45D',
        });
    }
});

我也使用了boostrap,这就是我用于日期选择器的东西。

PD:我试图为此创造一个小提琴,但出于某种原因,我无法使其发挥作用。

希望你能帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

我为datepicker添加了destroy和refresh。

JS:

$('#comuna_select').change(function(){
    $("#datepicker").datepicker("destroy");
    if($(this).val() == 'Cachapoal/Codegua'){
        function DisableCodegua(date) {
            var day = date.getDay();
            // If day == 1 then it is Monday
            if (day == 0 || day == 1 || day == 2 || day== 3 || day == 5 || day == 6 ) {
                return [false] ; 
            } else { 
                return [true] ;
            }
        }

        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            defaultdate: currentDate,
            changeMonth: true,
            changeYear: true,
            yearRange: "-2:+2",
            beforeShowDay: DisableCodegua,
            minDate: '-1D',
            maxDate: '+45D',
        });
    } else if ($(this).val() == 'Cachapoal/Coinco'){
        function DisableCoinco(date) {
            var day = date.getDay();
            // If day == 1 then it is Monday
            if (day == 0 || day == 1 || day == 2 || day== 3 || day == 4 || day == 6 ) {
                return [false] ; 
            } else { 
                return [true] ;
            }
        }

        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            defaultdate: currentDate,
            changeMonth: true,
            changeYear: true,
            yearRange: "-2:+2",
            beforeShowDay: DisableCoinco,
            minDate: '-1D',
            maxDate: '+45D',
        });
    } $( "#datepicker" ).datepicker("refresh");
});

现在一切正常。