php - 禁用 jquery datepicker 日历天,基于 ajax php mysql 表

标签 php jquery mysql ajax json

当我点击日期选择器时,我已经有一个工作正常,带有“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.iDateid 减去单词 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/

相关文章:

php - Laravel 转换数组仍然返回一个字符串

javascript - 将控件从容器中拖放到 Canvas 上

mysql - 开发一个从远程数据库获取内容的 iOS 应用程序

sql - 如何使用 group_concat 引用值

jquery - 使用jquery更新页面

MySql 表转成 JSON 格式

PHP如何检查MySQL数据库中已有的电子邮件?

php - 通过 AJAX 进行 SQL 更新查询的语法问题

javascript - 带有 WP_Query 的 Ajax Post Filter 仅针对类别触发一次

javascript - jQuery .then() 和 success block 不能一起工作?