从jQuery单击函数传递变量

时间:2015-04-13 08:37:52

标签: javascript jquery

我正在尝试从.click()函数传递函数但由于某种原因我没有得到该值。这是我的代码,

<script>
var guyid;

$(document).ready(function() {
    $('.guyid').click(function () {
        guyid = $(this).attr('id');  //This variable needs to pass
        alert(guyid);
    });
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicDay'
        },
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        eventSources: ['json-script.php?id=' + guyid]   //I need it here
    });
});

</script>

如何将变量从.click()函数传递给eventSources?非常需要这个帮助。 TNX。

1 个答案:

答案 0 :(得分:1)

您需要destroy fullcalendar并重新初始化它。

  

将元素恢复到FullCalendar初始化之前的状态。

$(document).ready(function() {
    $('.guyid').click(function() {
        var guyid = $(this).attr('id'); 

        $('#calendar')
            .fullCalendar('destroy') //Destroy existing calendar
            .fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicDay'
                },
                editable: true,
                eventLimit: true, 
                eventSources: ['json-script.php?id=' + guyid] //Set updated id
            });

    });
});

或者,您可以将events (as a function)refetchEvents

一起使用
var guyid = 'something';
$('#calendar').fullCalendar({
    events: function(start, end, timezone, callback) {
        $.ajax({
            url: 'json-script.php?id=' + guyid,
            dataType: 'JSON',
            success: function(doc) {
                var events = [];
                //Iterate are create 
                //This is pure hypothetical example
                $(doc).find('event').each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start') // will be parsed
                    });
                });
                callback(events);
            }
        });
    }
});

$('.guyid').click(function () {
    guyid = $(this).attr('id');  
    alert(guyid);

    $('#calendar').fullCalendar('refetchEvents');
});