gpt4 book ai didi

javascript - 从 jQuery 单击函数传递变量

转载 作者:行者123 更新时间:2023-12-03 10:21:39 25 4
gpt4 key购买 nike

我试图从 .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?非常需要这个帮助。交易。

最佳答案

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

Restores the element to the state before FullCalendar was initialized.

代码

$(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');
});

关于javascript - 从 jQuery 单击函数传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29601113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com