繁体   English   中英

将 jQuery UI 日期选择器与异步 AJAX 请求一起使用

[英]Use jQuery UI datepicker with async AJAX requests

我正在尝试在 jquery-ui 日期选择器中启用特定日期。 到目前为止,我已经设置了我的 sql 脚本和 json 文件,除了响应时间之外一切正常,因为我已将异步设置为 false。 我的 jquery 代码是。

var today = new Date();

$("#pickDate").datepicker({
    minDate: today,
    maxDate: today.getMonth() + 1,
    dateFormat: 'dd-mm-yy',
    beforeShowDay: lessonDates,
    onSelect: function(dateText) {
        var selectedDate = $(this).datepicker('getDate').getDay() - 1;
        $("#modal").show();
        $.get("http://localhost/getTime.php", {
            lessonDay: selectedDate,
            lessonId: $("#lesson").val()
        }, function(data) {
            $("#attend-time").html("");
            for (var i = 0; i < data.length; i++) {
                $("#attend-time").append("<option>" + data[i].lessonTime + "</option>");
                $("#modal").hide();
            }
        }, 'json');
    }
});

function lessonDates(date) {
    var day = date.getDay();
    var dayValues = [];
    $.ajax({
        type: "GET",
        url: "http://localhost/getLessonDay.php",
        data: {
            lessonId: $("#lesson").val()
        },
        dataType: "json",
        async: false,
        success: function(data) {
            for (var i = 0; i < data.length; i++) {
                dayValues.push(parseInt(data[i].lessonDay));
            }
        }
    });
    if ($.inArray(day, dayValues) !== -1) {
        return [true];
    } else {
        return [false];
    }
}

谁能帮我吗? 我重复上面的代码工作正常,但由于 async=false 而响应时间不好。

谢谢!

你做错了一切。 在您的示例中,该月的每一天都会触发一个同步AJAX请求。 您需要这样重构代码(粗略的概述):

// global variable, accessible inside both callbacks
var dayValues = [];

$("#pickDate").datepicker({
  beforeShowDay: function(date) {
    // check array and return false/true
    return [$.inArray(day, dayValues) >= 0 ? true : false, ""];
  }
});

// perhaps call the following block whenever #lesson changes
$.ajax({
  type: "GET",
  url: "http://localhost/getLessonDay.php",
  async: true,
  success: function(data) {
    // first populate the array
    for (var i = 0; i < data.length; i++) {
      dayValues.push(parseInt(data[i].lessonDay));
    }
    // tell the datepicker to draw itself again
    // the beforeShowDay function is called during the processs
    // where it will fetch dates from the updated array
    $("#pickDate").datepicker("refresh");
  }
});

在这里看到类似的例子

$.when(getdates()).done(function(){
            //this code is executed when all ajax calls are done => the getdates() for example
            $('#res_date').datepicker({
                startDate: '-0m',
                format: 'dd/mm/yyyy',
                todayHighlight:'TRUE',
                autoclose: true,
                datesDisabled: unavailableDates,
            });

        });

        function getdates(){
            return $.ajax({
                        type:'GET',
                        url:'/available_dates',
                        success:function(response){
                            for(let i = 0; i < response.data.length; i++){
                                unavailableDates.push(response.data[i]);
                            }
                            console.log(unavailableDates);
                            return unavailableDates;
                        },
                        
                    });            
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM