简体   繁体   English

jquery datepicker将变量从一个函数传递给另一个函数

[英]jquery datepicker pass variable from one function to another

i'm trying to pass the data i got from the function initiated by onChangeMonthYear to the function initiated by beforeShowDay. 我试图将我从onChangeMonthYear启动的函数中获得的数据传递给beforeShowDay启动的函数。 I tried various methods, like assigning the data I got from test.php to a variable declared at the outside but to no avail. 我尝试了各种方法,比如将我从test.php获得的数据分配给在外部声明的变量,但无济于事。 so what i did was just update a text field with the retrieved data from test.php, and asks the function in beforeShowDay to get the data from there. 所以我所做的只是使用来自test.php的检索数据更新文本字段,并要求beforeShowDay中的函数从那里获取数据。 here's a portion of my code 这是我代码的一部分

$(document).ready(function() {

    $('#datepicker').datepicker({dateFormat: 'dd-mm-yy', currentText: 'Now', changeMonth: true, changeYear: true, altField: '#alternate', altFormat: 'DD, d MM, yy', onSelect:  function(dateText, inst) {}, 
        onChangeMonthYear: function(year,month,inst){
            $.post("test.php", {action: "TEST", month: month, year: year}, 
            function (data){
                $("#datafromphp").val(data);
            }
            ,"html");
        }, 
        beforeShowDay: function (date){
            var getdatafromphp= $("#datafromphp").val();

        }
    });
});

surely there's a better way to do this? 肯定有更好的方法吗?

If I understand you correctly, you want to grab some data via AJAX asynchronously in onChangeMonthYear and make it accessible in beforeShowDay in order to display the updated data. 如果我理解正确,你想在onChangeMonthYear通过AJAX异步获取一些数据,并在beforeShowDay中访问它以显示更新的数据。

It helps to understand the order of the calls. 它有助于理解呼叫的顺序。 When the calendar first appears, a bunch of calls are made to beforeShowDay . 当日历首次出现时,会在beforeShowDay进行一系列调用。 At this stage, there is no data from your AJAX call yet (ie from test.php ). 在这个阶段,你的AJAX调用还没有数据(即来自test.php )。

Once the user changes the month/year, onChangeMonthYear will be called, and a bunch of calls are then made to beforeShowDay again. 一旦用户更改了月/年,将调用onChangeMonthYear ,然后再次对beforeShowDay进行一系列调用。 At this stage, there should still be no data from your AJAX call, unless the AJAX call to test.php manages to return quickly enough for some of the calls to beforeShowDay to access the returned value. 在这个阶段,你的AJAX调用仍然没有数据,除非对test.php的AJAX调用设法足够快地返回到beforeShowDay一些调用来访问返回的值。

Then the AJAX call returns and the returned data is somehow passed to beforeShowDay to display. 然后AJAX调用返回,返回的数据以某种方式传递给beforeShowDay以显示。

As you can see, what you tried to do would not work, as the AJAX call would not be on time for beforeShowDay to grab the data even if you "pass" it correctly. 正如你所看到的,你试图做的事情是行不通的,因为即使你正确地“传递”它,AJAX调用也不会在beforeShowDay上准时获取数据。 What you want to do is, assign the returned data from AJAX somewhere, and then refresh the datepicker, so beforeShowDay would be called again. 你想要做的是,从某处的AJAX分配返回的数据,然后刷新beforeShowDay ,这样beforeShowDay再次调用beforeShowDay You can store data to an arbitrary jQuery object with .data() . 您可以使用.data()将数据存储到任意jQuery对象。 So, you can do something like this: 所以,你可以这样做:

$('#datepicker').data('datafromphp', []).datepicker({dateFormat: 'dd-mm-yy', currentText: 'Now', changeMonth: true, changeYear: true, altField: '#alternate', altFormat: 'DD, d MM, yy', onSelect:  function(dateText, inst) {}, 
    onChangeMonthYear: function(year,month,inst){
        $.post("test.php", {action: "TEST", month: month, year: year}, 
        function (data){
            $("#datepicker").data('datafromphp', data).datepicker('refresh');
        }
        ,"html");
    }, 
    beforeShowDay: function (date){
        var getdatafromphp= $(this).data("datafromphp");

    }
});

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

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