Jquery Datepicker,束手无策
这段代码过去可以工作,但现在不行了,它只取数组中的第一个值...
var unavailableDates
是一个停止在日期选择器上显示日期的数组。
有什么想法吗?
由于某种原因,它没有循环遍历数组中的所有日期!?!?
var unavailableDates = ["4-7-2011","5-7-2011"];
function unavailable(date) {
var dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == 0) {
return [false, "", "Unavailable"];
} else {
var day = date.getDay();
return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
}
}
$(function(){
$('#smh').datepicker({
showOn: "both",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
beforeShowDay: unavailable,
minDate: -0,
dateFormat: "dd/mm/yy",
onSelect: function(e) {
e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
var date = new Date(e);
var day = date.getDay(); // 0 = sunday etc...
if (day === 1) {
$("#check2").hide();
$("#text").hide();
$("#check1").show();
} else if (day === 5) {
$("#check1").hide();
$("#text").hide();
$("#check2").show();
}
$("#bdate").html(this.value);
}
})
This code used to work but now it doesn't and it's only taking the first value in the array...
var unavailableDates
is an array that stops dates showing on the datepicker..
any ideas??
It's not cycling through all dates in the array for some reason!?!?
var unavailableDates = ["4-7-2011","5-7-2011"];
function unavailable(date) {
var dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == 0) {
return [false, "", "Unavailable"];
} else {
var day = date.getDay();
return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
}
}
$(function(){
$('#smh').datepicker({
showOn: "both",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
beforeShowDay: unavailable,
minDate: -0,
dateFormat: "dd/mm/yy",
onSelect: function(e) {
e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
var date = new Date(e);
var day = date.getDay(); // 0 = sunday etc...
if (day === 1) {
$("#check2").hide();
$("#text").hide();
$("#check1").show();
} else if (day === 5) {
$("#check1").hide();
$("#text").hide();
$("#check2").show();
}
$("#bdate").html(this.value);
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 jQuery 中,当在
array
中找不到elem
时,$.inArray(elem, array)
方法返回 -1,而不是 0。所以我认为你应该使用:
if ($.inArray(dmy
, unavailableDates) == -1)
而不是:
if ($.inArray(dmy, unavailableDates) == 0)
In jQuery the
$.inArray(elem, array)
method returns -1, and not 0, whenelem
is not found inarray
.So I think you should use:
if ($.inArray(dmy
, unavailableDates) == -1)
instead of:
if ($.inArray(dmy, unavailableDates) == 0)
发生的情况是
$.inArray
返回找到的项目的索引(如果找到,则为 -1),因此您需要检查索引是否不为 -1
What happens is
$.inArray
returns the index of the found item(if it finds it, else -1)so youll need to check if the index is not -1