2013-03-27 88 views
0

當我在日曆中放下某些東西時會調用它,並顯示一個調度對話框,當我按下該對話框中的按鈕時,它會調用ajax。爲什麼我的處理程序被多次調用?

我注意到,我第一次單擊它,1 ajax調用,然後按下,2 ajax調用,等等。

我只希望爲btnSchedule 1個處理器:

見:

$('#btnSchedule').on('click', function() { 

drop: function(date, allDay, ev, ui, resource) { // 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 event = $.extend({}, originalEventObject); 

        var obj = this; 

        var hours = 1; 

        var id = event.id; 
        var type = event.type; 


        $('#txtFrom').change(function() { 
         $('#txtHours').val(''); 
         fillPriorityList("#<%= ddlPriority.ClientID %>",type + id,resource.id,$('#txtFrom').val()); 
        }); 
        showScheduler(
         event.title, 
         $.fullCalendar.formatDate(date,'dddd MMMM dd yyyy'), 
         $.fullCalendar.formatDate(date, 'MM/dd/yyyy'), 
         event.canReoccur,event.reoccurVal,"#<%= ddlPeriod.ClientID %>", 
         "#<%= cbPeriod.ClientID %>"); 

        fillPriorityList("#<%= ddlPriority.ClientID %>",type + id,resource.id,$('#txtFrom').val()); 


        $('#btnSchedule').on('click', function() { 
         var hrs = $('#txtHours').val(); 
         var usingHrs = (isNaN(hrs) == false && hrs > 0); 
         var usingRange = isDateRangeValid('#txtFrom','#txtTo'); 
         var canRec = event.canReoccur; 
         var periodChecked = $("#<%= cbPeriod.ClientID %>").prop('checked'); 
         var recVal = $("#<%= ddlPeriod.ClientID %>").prop('selectedIndex'); 
         var priority = $("#<%= ddlPriority.ClientID %>").val(); 

         if(usingHrs || usingRange) 
         { 
          var f = $.datepicker.parseDate('m/d/yy', $('#txtFrom').val()); 
          var t = $.datepicker.parseDate('m/d/yy', $('#txtTo').val()); 
          var correctStart = usingHrs ? date : f; 
          $.ajax({ 
           type: "POST", 
           url: "CalendarServices.aspx/RegisterEvent", 
           data: 'id=' + id + '&start=' + $.fullCalendar.formatDate(correctStart, 'yyyy-MM-dd') 
            + '&end=' + $.fullCalendar.formatDate(t, 'yyyy-MM-dd') + '&type=' + type + '&hours=' + hrs + '&resource=' + resource.id 
           + '&canRec=' + canRec + '&periodChecked=' + periodChecked + '&recVal=' + recVal + '&priority=' + priority, 

           success: function (data) { 
            $(obj).remove(); 
            changeUnscheduledHeaders(); 
            closeScheduler();        
            $('#calendar').fullCalendar('refetchEvents'); 
           } 
    , 
           error: function() { 
           } 
          }); 
         } 

        }) 


       } 

       , 

我該怎麼做,以至於它只能調用我的ajax一次?

感謝

+3

因爲你不斷結合的Click事件。 – epascarello 2013-03-27 18:26:57

+0

在頂部有很多解釋,「當某些東西被丟棄時調用這個函數」,這意味着每次丟棄東西時都會註冊一個新的點擊處理程序。 – adeneo 2013-03-27 18:31:26

回答

3

再次嘗試

$('#btnSchedule').off('click').on('click'... 

結合,如果你把它綁定多次之前解除綁定click處理程序,回調會爲你綁定它每次都被執行。

+0

哦,我自己做了多少次! – CodeChimp 2013-03-27 18:32:42

相關問題