无法在AJAX成功函数中引用$(this)选择器

时间:2013-01-16 08:20:19

标签: jquery ajax fullcalendar selector

我想删除fullCalender中的事件,我遇到从日历中删除元素的问题。

我放的时候 在确认删除过程的条件之前 current.remove();,它运行良好并且事件被删除,但是当在成功方法中强制它时它不会影响元素,即使我正在兑现$(this) 选择器!

 $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month'
        },

        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar !
        events : url+"CalEvent",
        drop: function(date, allDay) { // this function is called when something is dropped

            // retrieve the dropped element's stored Event Object
            var originalEventObject = $(this).data('eventObject');

            // we need to copy it, so that multiple events don't have a reference to the same object
            var copiedEventObject = $.extend({}, originalEventObject);

            // assign it the date that was reported
            copiedEventObject.start = date;
            copiedEventObject.allDay = allDay;  

            //save event to db
            jQuery.ajax({
                url: url+"addCalEvent",
                type: "POST",
                dataType: 'JSON',
                data:{
                    'eventObj':copiedEventObject
                },

                error: function (request, status, error) {  
                    alert('خطأ في إضافة الحدث ');
                    revertFunc();
                }
            });

            // render the event on the calendar
            // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
            $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
                // if so, remove the element from the "Draggable Events" list
                $(this).remove();
            }

        } ,
//////////////////////////////////////////////////////
// here is the problem I face with deleting the event ////
        eventClick: function(event, element) {

        var current = $(this);

     if(confirm('هل متأكد من حذف الحدث')){
                jQuery.ajax({
                    url: url+"delCalEvent",
                    type: "POST",
                    dataType: 'JSON',
                    data:{
                        'eventId':event.id
                    },
                    success:function(){

                        current.remove();
                        $('#calendar').fullCalendar('updateEvent', event);
                    },
                    error: function (request, status, error) {  
                        alert('خطأ في حذف الحدث ');
                        revertFunc();
                    }
                });


            } 

        },

        eventDrop: function(event,dayDelta,minuteDelta,allDay,revertFunc) {
            //check for changes
            if (!confirm("هل أنت متأكد من تغيير موعد الحدث ؟")) {
                revertFunc();
            }else{
                //update current event 
                jQuery.ajax({
                    url: url+"updateCalEvent",
                    type: "POST",
                    dataType: 'JSON',
                    data:{
                        'eventObj':event
                    },

                    error: function (request, status, error) {  
                        alert('خطأ في تعديل الحدث ');
                        revertFunc();
                    }
                });
            }





        } 


    });

1 个答案:

答案 0 :(得分:0)

您可以使用fullCalendar remove()删除事件,而不是使用JQuery updateEventremoveEvents

 eventClick: function(event, element) {

     var current = event;

     if(confirm('هل متأكد من حذف الحدث')){
            jQuery.ajax({
                url: url+"delCalEvent",
                type: "POST",
                dataType: 'JSON',
                data:{
                    'eventId':event.id
                },
                success:function(){
                    $('#calendar').fullCalendar('removeEvents', current.id);
                },
                error: function (request, status, error) {  
                    alert('خطأ في حذف الحدث ');
                    revertFunc();
                }
            });
       } 
    },