2014-11-25 83 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); 
      } 
     } 
    });