首页 > 解决方案 > 根据引导日期选择器中的下拉选择预选择日期?

问题描述

任何人都知道,我可以如何实现以下目标:

  1. 如果有人选择“仅限周末”选项,则所有周末日期都将选择从当前月份到下个月的指定日期。
  2. 如果有人选择“仅适用于工作日”选项,则将选择从当前月份到下个月的指定日期的所有工作日日期。
  3. 如果有人选择“仅限周五”选项,则将选择从当前月份到下个月的指定日期的所有周五日期。

类似上面的东西: 在此处输入图像描述

有人知道我可以从哪里开始吗?

$('#dt_1').datepicker({
        rtl: KTUtil.isRTL(),
        todayHighlight: true,
        templates: arrows,
        startDate: date, //disable all old dates
        setDate: date, //tomorrow's date allowed
        multidate: true,
        format: 'dd/mm/yyyy'

    });

标签: phpjquerybootstrap-datepicker

解决方案


在另一篇stackoverflow帖子的帮助下,我找到了实现这一目标的解决方案: 使用php获得周末

PHP代码:

//weekend.....
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekend_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
    $day_index = date("w", $now);
    if ($day_index == 0 || $day_index == 6) {  
        // Print or store the weekends here.  
        //echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
        $weekend_array[] = date("d-m-Y", $now);
    }
    $now = strtotime(date("d-m-Y", $now) . "+1 day");
}

//friday only....               
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$friday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
    $day_index = date("w", $now);
    if ($day_index == 5) {  
        // Print or store the weekends here.  
        //echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
        $friday_array[] = date("d-m-Y", $now);
    }
    $now = strtotime(date("d-m-Y", $now) . "+1 day");
}

//weekdays......                  
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
    $day_index = date("w", $now);
    if ($day_index >= 1 && $day_index <= 5) {  
        // Print or store the weekends here.  
        //echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
        $weekday_array[] = date("d-m-Y", $now);
    }
    $now = strtotime(date("d-m-Y", $now) . "+1 day");
}

Javascript代码:

$(document).ready( function () {

$("#sel_dropdown").change(function() {
    $( "select option:selected" ).each(function() {
        var v = $(this).val();
        if(v == 1) {
            //weekend only....
            $('#dt_1').datepicker('setDates',[<?php 
                $i=0;   
                for($i=0;$i<=count($weekend_array);$i++) {
                    echo "'".$weekend_array[$i]."',";
                }
            ?>]);
        }
        if(v == 2) {
            //Friday only.....
            $('#dt_1').datepicker('setDates',[<?php 
                $i=0;   
                for($i=0;$i<=count($friday_array);$i++) {
                    echo "'".$friday_array[$i]."',";
                }
            ?>]);
        }
        if(v == 3) {
            //Weekdays only....
            $('#dt_1').datepicker('setDates',[<?php 
                $i=0;   
                for($i=0;$i<=count($weekday_array);$i++) {
                    echo "'".$weekday_array[$i]."',";
                }
            ?>]);
        }
        if(v == 4) {
            //custom option....
            $('#dt_1').data('datepicker').setDate(null); 
        }
    });

});

});
</script>   

希望这对寻找此类解决方案的任何人都有用。


推荐阅读