2014-03-28 36 views
3

我有以下的JavaScript來的工作,並且只在Chrome的工作,而且我想不通爲什麼:toElement似乎只在Chrome,IE不FF或

//makes appointments draggable 
    $("._ts").sortable({ 
     connectWith: "._ts", 
     revert: "true", 
     cancel: ".new_appt", 

    stop: function(e){ 
     var element = e.toElement; 
     var date = $(element).parents('.route_container').find('.date h2').html(); 
     var timeslot = $(element).parents('.timeslot').attr('id'); 
     var tAppt_id = $(element).attr('id'); 
     console.log("Date:."+date); 
     console.log("time:."+timeslot); 
     console.log("route:."+tAppt_id); 

     $.ajax({ 
      type: "post", 
      dataType: "json", 
      url: ajaxurl, 
      data:{action: "update_appointments", date: date, timeslot: timeslot, appt_id: tAppt_id}, 
      success: function(response){ 
       if(response.type == "success"){ 
        console.log("Update appointment worked."); 
        console.log("Date:."+response.date); 
        console.log("time:."+response.timeslot); 
        console.log("route:."+response.timeslot); 



        $(this).parents('.delete_appt').hide(); 
       } 
      } 
     }); 
    } 

}); 

的問題是,變量date,timeslot,& tAppt_id返回爲undefined。這再次適用於Chrome;但僅限於Chrome。不適用於IE或FF。

我也試過使用e.currentTargete.relatedTarget都沒有工作。有人能告訴我我做錯了什麼嗎?

回答

5

看起來像你使用jQuery-UI Sortable。在這種情況下,您將獲得jQuery event對象作爲事件處理程序的第一個參數。要獲得事件目標元素,您需要使用target屬性:

var element = e.target; 
+0

這就是它的人!感謝您的幫助! – BlackHatSamurai

+0

我剛剛意識到,你給我的答案獲得了原點元素,我將如何獲得項目被刪除的元素? – BlackHatSamurai

+0

根據[documentation](http://api.jqueryui.com/sortable/#event-stop)它將來自處理程序的'ui.item'第二個參數'stop:function(e,ui){'。 –

相關問題