2014-11-21 47 views
0

我必須製作Datepicker,其工作方式如下:如果所選月份具有偶數天數(30),則該月中的所有偶數天將被阻止選擇(2,4,6 ...)等。如果選擇的月份具有奇數天數(31),則日期選擇器中的所有奇數日將被阻止以選擇(1,3,5 ...)等。甚至可以在偶數天內阻止偶數天(JQuery UI)

我的問題是,我不知道如何阻止偶數和奇怪的日子,我知道只有如何阻止整個月,但不是特別的一天... 聲明:我知道有已經發布瞭如何阻止某一天,但不是偶數或奇數日。

這裏是我的代碼:

<link rel="stylesheet" href="jquery-ui.min.css"> 
    <script src="external/jquery/jquery.js"></script> 
    <script src="jquery-ui.min.js"></script> 
    <script> 
    $(function() { 
    $("#datepicker").datepicker({beforeShowDay: nationalDays}); 
    }); 

function nationalDays(date){ 
var x = date.getMonth(); 
var r = x%2; 
if(r == 0){ 
    return [false]; 
} 
else if(r == 1){ 
    return [true]; 
} 
} 
</script> 
</head> 
<body> 

<p>Date: <input type="text" id="datepicker"></p> 


</body> 

回答

1

裏面beforeShowDay選項,您可以檢查日期是否應啓用或不和數組中設置的第一項被返回給true/false。真將啓用該項目,而false將禁用它,如下圖所示:

$(function() { 
 
    $("#datepicker").datepicker({ 
 
    defaultDate: new Date(), 
 
    beforeShowDay: function(date) { 
 
     var disabled = true, // date enabled by default 
 
     // get the number of days in current month 
 
     numOfDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); 
 
     if (numOfDays % 2 == 0) 
 
     disabled = (date.getDate() % 2 != 0) // for even-days months, disable the even dates 
 
     else disabled = (date.getDate() % 2 == 0) //for odd - days months, disable the odd dates 
 
     return [disabled, ""] 
 
    } 
 
    }); 
 
});
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
 
<p>Date: 
 
    <input type="text" id="datepicker" /> 
 
</p>

+1

謝謝您,可以作爲一個魅力:) – user3075088 2014-11-21 19:51:54