从JQuery datepicker禁用周末和前几天

时间:2014-05-21 11:07:15

标签: jquery html5

<script>
    $(function() {
        $.datepicker.setDefaults({dateFormat: 'yy-mm-dd'});
        $("#datepicker1").datepicker({ beforeShowDay: $.datepicker.noWeekends });
        $( "#datepicker1" ).datepicker({ minDate: 0});
        $( "#datepicker1" ).datepicker();                   
    });
</script>

我有上面的代码,我想从我的JQuery datepicker中禁用周末和以前的日期..但它的作用是它只会禁用周末而不是过去几天。我哪里出错???

4 个答案:

答案 0 :(得分:1)

您可以尝试这样

$(function() {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentDate = date.getDate();
    var currentYear = date.getFullYear();
   $('#txtDate').datepicker({ 
       minDate: new Date(currentYear, currentMonth, currentDate),  // will disable past days
       beforeShowDay: $.datepicker.noWeekends // Will disable weekends
   });
});

Fiddle Demo

文档

  • minDate
  • beforeShowDay
  • 答案 1 :(得分:1)

    将所有设置传递给一个电话

    $(function () {
        $.datepicker.setDefaults({
            dateFormat: 'yy-mm-dd'
        });
        $("#datepicker1").datepicker({
            beforeShowDay: $.datepicker.noWeekends,
            minDate: 0
        });
    });
    

    答案 2 :(得分:1)

    <强>小提琴:

    <强> http://jsfiddle.net/rLnTQ/877/

    您可以一次性使用所有选项,而不是单独执行。 你多次调用datepicker,这是不需要的。

    $(function() {
       $.datepicker.setDefaults({
       // here we can have all the common properties which we need for all the datepickers
           dateFormat: 'yy-mm-dd',
           duration:"slow"
       });
    
       $('#datepicker1').datepicker({ 
           minDate:0,
           dateFormat: 'yy-mm-dd', // this is for single datepicker.
           beforeShowDay: $.datepicker.noWeekends
       });
    });
    

    答案 3 :(得分:0)

    每次指定一个选项时,你都会重新初始化datepicker,以实现这两个要求,这样做

    JQUERY CODE:

                  $("#datepicker1").datepicker({
                      minDate: 0,
                      beforeShowDay: $.datepicker.noWeekends
                  });
    

    现场演示: http://jsfiddle.net/dreamweiver/XsG27/3/

    快乐编码:)

    相关问题