2014-11-25 82 views
0

我使用jquery UI datepicker示例站点中的脚本(1)。它提供了选择数据范围的功能。我添加了“minDate:0”行,禁用了今天之前的所有日期。 我想将它与我在Stack Overflow中找到的函数(2)结合起来。它禁用日历中的日期数组。Jquery UI datepicker合并函数

我试图结合这些脚本,但它不能使它们一起工作(选择日期范围,禁用今天之前的日期并禁用日期数组)。 帮助将非常感激。 (1)jquery UI datepicker示例站点中的代码。这pen有一个工作示例。

$(function() { 
    $("#from").datepicker({ 
     minDate: 0, 
     changeMonth: true, 
     numberOfMonths: 2, 
     onClose: function(selectedDate) { 
     $("#to").datepicker("option", "minDate", selectedDate); 
     } 
    }); 
    $("#to").datepicker({ 
     minDate: 0, 
     changeMonth: true, 
     numberOfMonths: 2, 
     onClose: function(selectedDate) { 
     $("#from").datepicker("option", "maxDate", selectedDate); 
     } 
    }); 
    }); 


<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"> 
</head> 

<body> 

<label for="from">From</label> 
<input type="text" id="from" name="from"> 
<label for="to">to</label> 
<input type="text" id="to" name="to"> 


</body> 

(2)脚本禁用某些日期(link)(另见本fiddle

var array = ["2013-03-14","2013-03-15","2013-03-16"] 

$('input').datepicker({ 
    beforeShowDay: function(date){ 
     var string = jQuery.datepicker.formatDate('yy-mm-dd', date); 
     return [ array.indexOf(string) == -1 ] 
    } 
}); 
+0

这是[小提琴]( http://jsfiddle.net/pz2s746h/)你想要什么? – sb9 2014-11-25 10:02:42

+0

这正是我想要的。非常感谢。我尝试过这样的代码混合,但一定忽略了一些东西(缺乏技能但是在工作中)。 – Ixillion 2014-11-25 10:31:24

回答

0

就结合这些选项是这样的:

var array = ["2014-12-14","2014-12-15","2014-12-16"]; 

$("#from").datepicker({ 
     minDate: 0, 
     changeMonth: true, 
     numberOfMonths: 2, 
     beforeShowDay: function(date){ 
     var string = jQuery.datepicker.formatDate('yy-mm-dd', date); 
     return [ array.indexOf(string) == -1 ] 
     }, 
     onClose: function(selectedDate) { 
      if (selectedDate) { 
      $("#to").datepicker("option", "minDate", selectedDate); 
      } 
     } 
    }); 
$("#to").datepicker({ 
     minDate: 0, 
     changeMonth: true, 
     numberOfMonths: 2, 
     beforeShowDay: function(date){ 
     var string = jQuery.datepicker.formatDate('yy-mm-dd', date); 
     return [ array.indexOf(string) == -1 ] 
     }, 
     onClose: function(selectedDate) { 
      if (selectedDate) { 
      $("#from ").datepicker("option", "maxDate", selectedDate); 
      } 
     } 
    });