2015-05-09 55 views
0

這是我handler爲DIV Click處理程序不jQuery的完整的日曆工作

$("div.fc-content").on('click', function (e) { 
     var id = $(this).data('id'); 
     $.ajax({ 
      url: '/VacationRequest/Edit/' + id, 
      cache: false 
     }).done(function (data) { 
      $("#edit-id").val(data.Id); 
      $("#startDate-edit").val(data.startDate); 
      $("#endDate-edit").val(data.endDate); 
      $("#comment-edit").val(data.comment); 
      $("#vacationTypes-edit").val(data.vacationType); 
      $('#edit-vacation-modal-window').modal('show'); 
     }); 
    }); 

這是Jquery完整的日曆初始化的一部分:

function InitCalendar() { 
    $('#calendar').fullCalendar({ 
     events: { 
      url: '/vacation/SchedulesGet', 
      type: 'POST', 
      data: { 
       title: 'title',      
       id : 'id', 
       start: new Date('start'), 
       end: new Date('end'), 
      }, 
      error: function() { 
       alert('there was an error while fetching events!'); 
      }, 
      color: 'yellow', 
      textColor: 'black' 
     }, 
     eventRender: function (event, element) { 
      $($(element[0]).find('div.fc-content')[0]).attr('data-id', event.id); 
     }, 

$("div.fc-content")塊是存在(我在js控制檯中找到它)。但是當我試圖點擊它時,什麼都沒發生。你能解釋我做錯了什麼嗎?

回答

1

嘗試使用委派的事件處理程序,像這樣:事件代表團

$('#calendar').on('click', 'div.fc-content', function (e) { 

    ... 

}); 

更多信息:https://learn.jquery.com/events/event-delegation/

或綁定您的onclickeventRender函數內部:

$('#calendar').fullCalendar({ 
    ... 
    eventRender: function (event, element) { 

     element.click(function() { 
      ... 
     }); 
    } 
}); 
+0

非常感謝!是工作!但你能解釋我嗎?爲什麼?我不明白( – Alexander

+0

更新了答案。基本上使用一個委託事件處理程序允許你處理*在你實際綁定處理程序後創建的DOM元素的事件 – dekkard

+0

好吧,據我所知,有必要把處理程序這是一個問題,對嗎? – Alexander