问题描述
我有与FullCalendar一个问题,我一直在寻找没有任何成功的解决方案。我用eventClick与当前事件的数据打开叠加的形式。一切都很正常,直到我改变了主意,不想编辑该事件,但一个又一个。而这导致的ajax发送请求2次,一次打开事件(这是准备编辑,但形式没有提交),另一次事件,是真的编辑和提交。
I've got a problem with FullCalendar and I was looking for solution without any success.I use eventClick to open overlay form with data of current event. Everything works great until I change my mind and don't want to edit this event but another one. And that causes ajax send request 2 times, once for opened event (which was ready to edit but form was not submitted) and once for event which was really edited and submitted.
$('#sc-calendar').fullCalendar({
eventClick: function(event) {
//opening overlay form window
$('#submit-event-update').bind('click',function() { //click submit
$.ajax({
type: "GET",
url: "event_update",
data: "id="+event.id+"&title="+event.title+"&start="+event.start+"&end="+event.end,
cache: false,
success: function() {
$('#submit-event-update').unbind();
$('#sc-calendar').fullCalendar('updateEvent',event);
}
});
});
}
});
这已经开始成为我的噩梦。请帮帮忙!
This is starting to be my nightmare. Please help!
推荐答案
在我看来,像有与的onclick
事件侦听器问题#提交事件更新
。
It seems to me like there is a problem with the onclick
event listener for #submit-event-update
.
您应该修改code以这种方式:
You should modify your code in this way:
$('#sc-calendar').fullCalendar({
eventClick: function(event) {
//opening overlay form window
//Assign the event id of this event to the global variable event_id
event_id = event.id;
}
});
$('#submit-event-update').bind('click',function() { //click submit
$.ajax({
type: "GET",
url: "event_update",
data: "id=" + event_id + ..., //Note how event.id is not used anymore
cache: false,
success: function() {
$('#sc-calendar').fullCalendar('updateEvent',event);
}
});
});
我改变了它,让你的的onclick
事件处理程序绑定到该按钮只有一次,而不是每一个事件被点击的时间。我还指定一个变量 EVENT_ID
保存当前事件的id的值。
I changed it so that you bind the onclick
event handler to the button just once as opposed to every time an event is clicked. I also assign a variable event_id
that holds the value of the current event's id.
现在,一个解释。你说:
Now, for an explanation. You said:
一切都很正常,直到我改变了主意,不想编辑 这个事件,但一个又一个。
当你点击一个事件发生了什么?
What happens when you click on an event?
您绑定的的onclick
事件的#提交事件更新
。现在,如果你点击了按钮,那么你将进入成功()
回调的AJAX调用,然后解除绑定按钮。但是,如果你改变主意,不点击提交按钮?现在,你有一个的onclick
监听器已经绑在按钮的旧数据。当你拿起另一个事件,你有两个事件侦听器连接到同一个按钮,因此,一个AJAX请求被发送两次
You bind the onclick
event to the #submit-event-update
. Now if you do click the button, then you will go into the success()
callback of your AJAX call and then unbind the button. But what if you change your mind and don't click the submit button? Now, you have one onclick
listener with the old data already tied to the button. When you pick another event, you have two event listeners tied to the same button and hence, an AJAX request is sent two times.
这可能是值得JavaScript的如何处理事件绑定here.
It might be worth reading up on how JavaScript handles event binding here.
这篇关于事件对象持有的全部日历previous事件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!