当我点击日期选择器时,我已经有一个工作正常,带有“beforeShowDay”函数,我触发了一个 $.getJSON PHP 过程,它读取一个 MySQL 表,创建一个数组,并使用另一个函数,我禁用了日期在我想要的数据选择器日历上。这工作正常,只适用于一个日历。现在我需要在 PHP/MySql 列表上生成的多个日历上禁用 datepickers 天。这是我的代码,希望你理解我并给我帮助!
<script>
var unavailableDates = [];
$(document).ready(function(){
var myTour = "<?php echo $tourid ;?>";
$.getJSON('closed_dates.php',{
tourid: myTour
},
function(data) {
unavailableDates = data;
}); });
function unavailable(date) {
ymd = date.getFullYear() + "-" + ("0"+(date.getMonth()+1)).slice(-2) + "-" + ("0"+date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if ($.inArray(ymd, unavailableDates) < 0) {
return [true, "enabled", "Book Now"];
} else {
return [false,"disabled","Sold Out"];
}
}
enter code here$(function() {
$('.iDate').datepicker({
dateFormat: 'dd-mm-yy',
minDate: 0,
beforeShowDay: unavailable,
});});</script>
如果只有一个日历,此代码可以工作,但我需要能够从列表中禁用日期选择器日历日,这是我的 PHP 代码
$query_tours = "Select * from tours where status ='A' and featured = 'Y' order by rand() limit 0,20" ;
$result_tours = mysql_query($query_tours);
while($row=mysql_fetch_array($result_tours )) {
$tourid = $row['recid'];
$tit_ing = $row['tit_ing'];
(some html/css code here .....)
print "<input type='number' name='adults'>";
print "<input type='number' name='children'>";
print "<input type='text' name='tourdate' class='iDate' id='date$tourid'>";
}
所以我不知道如何从 php(列表)传递 id 值并作为 $.getJSON 上“myTour”的值发送给每个日期选择器日历。
希望,有人理解我,最好的问候,
最佳答案
示例中的 tourid
是 .iDate
的 id
减去单词 date
,这可能是像这样访问:
$(this).id.substr(4);
由于您预加载了 unavailableDates
,它可能是一个由此 tourid
索引的数组。
var unavailableDates = [];
$(document).ready(function(){
$('.iDate').each(function () { // For each iDate object
var myTour = $(this).id.substr(4); // Get tourid from iDate id
$.getJSON('closed_dates.php',{
tourid: myTour
},
function(data) {
unavailableDates[myTour] = data; // Add to the array
}); }); });
然后在函数 unavailable
中(顺便说一句,这不是一个好名字)你可以通过 this 的
,被后台代码设置为id
索引 unavailableDates
数组'date$tourid'
。
function unavailable(date) {
var myTour = $(this).id.substr(4); // Get the tourId
...
if ($.inArray(ymd, unavailableDates[myTour]) < 0) {
...
}
并不是说这会触发与日历一样多的 ajax 请求。由于您正在预加载,因此最好将数组直接包含在第一个 PHP 页面中,如下所示:
print "<script>unavailableDates[$tourid]=$getDates(tourid);</script>";
关于php - 禁用 jquery datepicker 日历天,基于 ajax php mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20925587/