jQuery:如何将值从一个函数传递到另一个函数?

时间:2017-10-14 00:50:23

标签: jquery

更新:

以下是我试图找出的简化版本(请参阅评论中的问题):

        $("document").ready(function() {
        $("#startDate").datepicker({

            onSelect: function(dateText, inst) { 
                var dateAsObject = $(this).datepicker( 'getDate' );
                var timestamp = dateAsObject.getTime(); // the timestamp of the date picked by user
            }
        });

        // how do I get the value of timestamp to show up here?
        // It also needs to update every time onSelect changes the value.
        alert(timestamp);

    });

2 个答案:

答案 0 :(得分:1)

由于这是异步流程,因此您必须处理事件。

这样的事情应该可行:

$("document").ready(function() {

     function setClock(startTime) {
        return $('.clock').FlipClock(
            (new Date()).getTime() / 1000 - startTime, 
            {
                clockFace: 'DailyCounter'
            }
        );
    }


    $("#startDate").datepicker({

        // ... all your (here irrelevant) options ...

        onSelect: function(dateText, inst) { 
            // I assume this is an event handler that gets called every time your date changes. 
            // Here you have to update the timestamp on your second plugin.
            setClock($(this).datepicker( 'getDate' ).getTime());
    });

    // initiate the clock
    setClock((new Date(2017, 8, 11)).getTime());

});

首先,我声明一个可以使用提供的日期设置时钟的函数。然后我设置了datepicker,它是(假设的)更改处理程序我告诉它用新选择的日期更新时钟。最后,我启动时钟,以便在用户选择日期之前已经显示了一些内容(您应该使用与日期选择器的起始值相同的日期)。

理想情况下,您可以更新时钟而不是重新启动它,但我不知道插件API支持这一点。我不知道这是否真的有效,因为我不知道实际的插件,也无法测试。我希望它能指出你正确的方向。随意询问是否有任何不清楚的地方!

答案 1 :(得分:-1)

如果两个函数都在同一个文件中,则可以直接使用变量 myFooList[0].FooLength = 3; strcpy( myFooList[0].FooChar, "dog"); myFooList[1].FooLength = 3; strcpy(myFooList[1].FooChar, "cat"); 因为它是全局定义的。 如果您从单独的文件中调用,则可以使用var timestamp Tag在其他文件中调用变量。

相关问题