jQuery 日期选择器禁用星期日 &特定的日期数组 [英] jQuery datepicker disable sundays & specific array of dates

查看:22
本文介绍了jQuery 日期选择器禁用星期日 &特定的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经讲得很远了,但还是坚持到了最后一部分.我基本上只需要禁用所有星期日,然后能够将日期添加到数组中也被禁用,我知道单独禁用每个的代码,但是当我尝试将它们放在一起时,我要么得到带有可选日期数组的星期日,要么另一种方式.我需要加入以禁用数组和星期日的两个部分在 beforeshowdays 函数中,我知道我不能有两个我只是把两个都放进去,这样你就可以看到我在尝试什么.感谢您的帮助

I've gotten pretty far with this but stuck on the last part. I basically just need to disable all sundays and then be able to add dates to an array to also be disabled, I know the code to disable each individually but when I try to put them together I either get sundays with the array of dates selectable or the other way around. The two parts I need to join to have the array and sundays disable are in the beforeshowdays functions, I know I can't have two I just put both in so you can see what I'm trying. Thanks for any help

<script>
    var array = ["2016-08-29","2013-03-15","2013-03-16"]
</script>

<script>  

$( function() {

var today = new Date();

$( "#datepicker" ).datepicker({
    dateFormat: 'dd MM yy',
    beforeShowDay: function(date) {
        var day = date.getDay();
        return [(day != 0), ''];
    },
    beforeShowDay: function(date) {
       var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
    },
    //beforeShowDay: $.datepicker.noWeekends,
    firstDay: 1,
    minDate: today.getHours() >= 14 ? 2 : 1,
    maxDate: '+1m',
    onSelect: function(dateText, inst) { 
        $('#checkout_attribute_1').val(dateText);
    }
  });
 } );
</script>

推荐答案

你可以试试这样的:

var array = ["2016-08-29", "2013-03-15", "2013-03-16"]

$(function() {
  var today = new Date();
  $("#datepicker").datepicker({
    dateFormat: 'dd MM yy',
    beforeShowDay: function(date) {
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(array,string)) {
        debugger;
        return [false, '']
      } else {
        var day = date.getDay();
        return [(day != 0), ''];
      }
    },
    //beforeShowDay: $.datepicker.noWeekends,
    firstDay: 1,
    minDate: today.getHours() >= 14 ? 2 : 1,
    maxDate: '+1m',
    onSelect: function(dateText, inst) {
      $('#checkout_attribute_1').val(dateText);
    }
  });
});

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

https://jsfiddle.net/9hrhyd4q/

这篇关于jQuery 日期选择器禁用星期日 &amp;特定的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆