gpt4 book ai didi

jquery - 在 jQuery UI 日期选择器中仅选择特定日期(日期列表来自 AJAX)

转载 作者:行者123 更新时间:2023-12-03 22:31:37 25 4
gpt4 key购买 nike

在这里,我发送电影 ID 并获取可用天数,我想将其设置到日历中。但它不起作用并且它禁用所有日期。它从 PHP 返回日期字符串。日期字符串正确,但日历不起作用。请帮忙。

日期字符串示例

"28-02-2012","29-02-2012","01-03-2012","02-03-2012","03-03-2012","04-03-2012","05-03-2012","06-03-2012","07-03-2012","08-03-2012","09-03-2012","28-02-2012","29-02-2012","01-03-2012","02-03-2012","03-03-2012","04-03-2012","05-03-2012","06-03-2012","07-03-2012","08-03-2012","09-03-2012"

代码

jQuery.post('index.php', {
'option': 'com_movie',
'controller': 'reservation',
'task': 'datelist',
'format': 'raw',
'mid': movieid
}, function(result) {
var onlydates = result.split(',');
jQuery("#datepicker").datepicker({
dateFormat: 'yy-mm-dd',
showOn: "button",
buttonImage: "<?php echo IMAGES_LINK.'calendar.png';?>",
buttonImageOnly: true,
beforeShowDay: function(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
console.log(dmy + ' : ' + (jQuery.inArray(dmy, onlydates)));
if (jQuery.inArray(dmy, onlydates) != -1) {
return [true, "", "Available"];
} else {
return [false, "", "unAvailable"];
}
}
});
return;
});

最佳答案

  • 仅初始化一次datapicker;告诉它从全局数组中获取有效日期
  • 使用数组文字初始化全局数组,必要时通过 AJAX 更新它
  • 每当禁用/启用日期发生更改时(即通过 AJAX 获取新结果时),调用 .datepicker("refresh") 方法
  • 您的代码不会向日期添加前导零,因此 $.inArray 将无法按预期工作
<小时/>
var datelist = []; // initialize empty array

$("#datepicker").datepicker({
beforeShowDay: function(d) {
// normalize the date for searching in array
var dmy = "";
dmy += ("00" + d.getDate()).slice(-2) + "-";
dmy += ("00" + (d.getMonth() + 1)).slice(-2) + "-";
dmy += d.getFullYear();
return [$.inArray(dmy, datelist) >= 0 ? true : false, ""];
}
});

$("#button").click(function() {
datelist = []; // empty the array
$.post("/echo/html/", {
// parameters here
}, function() {
var result = "28-02-2012,29-02-2012,01-03-2012,02-03-2012,03-03-2012,04-03-2012,05-03-2012,06-03-2012,07-03-2012,08-03-2012,09-03-2012,28-02-2012,29-02-2012,01-03-2012,02-03-2012,03-03-2012,04-03-2012,05-03-2012,06-03-2012,07-03-2012,08-03-2012,09-03-2012"; // dummy result
datelist = result.split(","); // populate the array
$("#datepicker").datepicker("refresh"); // tell datepicker that it needs to draw itself again
});

Demo here

关于jquery - 在 jQuery UI 日期选择器中仅选择特定日期(日期列表来自 AJAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9480537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com