簡體   English   中英

通過jQuery click函數傳遞變量

[英]Pass variable from jQuery click function

我正在嘗試從.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>

如何將變量從eventSources .click()函數傳遞給eventSources 急需此幫助。 TNX。

您需要destroy日歷並重新對其進行初始化。

將元素恢復到初始化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');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM