jQuery datepicker-beforeShowDay问题 [英] jquery datepicker - beforeShowDay problem
问题描述
我有以下代码:
$(document).ready(function(){
$(".datepicker").each(function() {
$(this).datepicker({
beforeShowDay: function(date){ return [$(this).parent().find("span.days").html(), ""] },
});
});
});
使用此正文代码...
With this body code...
<div>
<span class="days">(date.getDay() == 1 || date.getDay() == 4)</span>
<input type="text" class="datepicker">
</div>
<div>
<span class="days">(date.getDay() == 2 || date.getDay() == 5)</span>
<input type="text" class="datepicker">
</div>
我想做的是让每个日期选择器有不同的可采日期.
What I want to be able to do is have each datepicker have different days pickable.
我尝试执行此操作的方式(在跨度中放入beforeShowDay代码的不同部分)可能不是最优雅的,所以请随时将我的代码拆开并在必要时进行更改.
The way i'm trying to do this (putting the varying part of the beforeShowDay code in a span) may not be the most elegant so feel free to take my code apart and change it if necessary.
推荐答案
您可以创建一个保留可挑选日期的对象,如下所示:
You could create an object keeping the pickable days, like this:
var pickable = { dp1: [1, 4], dp2: [2, 5] };
$('.datepicker').each(function() {
$(this).datepicker({
beforeShowDay: function(date){
var day = date.getDay(), days = pickable[this.id];
return [$.inArray(day, days) > -1, ""];
}
});
});
您可以在此处尝试. dp1
和dp2
是控件的ID ..或您可以真正映射的任何属性,例如:
You can give it a try here. The dp1
and dp2
are IDs of the controls..or any attribute you can map really, like this:
<input type="text" class="datepicker" id="dp1">
<input type="text" class="datepicker" id="dp2">
这个概念非常简单,获取ID以获取该选择器的天数,然后使用 $.inArray()
来查看我们所在的日子是否在该数组中.如果这些天的选择可以共享,请不要使用ID,而是对属性进行data-pickerType
之类的操作,并在上面的代码中将this.id
替换为$(this).attr("data-pickerType")
.
The concept is pretty simple, take the ID to get the array of days for this picker, then use $.inArray()
to see if the day we're on is in that array. If these day selections may be shared, don't use an ID, instead do something like data-pickerType
for the attribute, and replace this.id
with $(this).attr("data-pickerType")
in the code above.
注意:我完好无损地保留了 .each()
您出于当前问题之外的原因需要它.
Note: I left the .each()
intact because I know you need it for reasons outside the current question.
这篇关于jQuery datepicker-beforeShowDay问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!