2010-06-23 83 views
5

我試圖用Jquery進行類別更改& Php。我沒有問題。我的問題是,當更新事件被調用時,它返回2個結果。 1個拖拽父項的結果,1個刪除父項的結果。我想只打電話給父母的身份證。這是我的腳本:Jquery Sortable Update Event只能調用一次?

$("#gallery ul").sortable({ 
    connectWith: '.dropBox', 
    opacity: 0.35, 
    scroll: true, 
    scrollSensitivity: 100, 
    //handle: '.move', 
    helper: 'clone', 
    containment:'#gallery', 
    accept:'#gallery > .photo', 
    revert: true, 
    update: function(event, ui){ 
     params = 'c=' + $(this).attr('id') + '&id=' + ui.item.attr('id'); 

     $.ajax({ 
      type: 'POST', 
      url: 'processData.php', 
      data: params, 
      error:function(){ 
       alert("Error!"); 
      }, 
      success:function(data){ 
       $("#serverResponse").html(data); 
      } 
     }); 
    } 
}).disableSelection(); 

你能幫助我嗎?

回答

7

使用updatestopreceive事件,例如

$(function() { 
    position_updated = false; //flag bit 

    $(".sortable").sortable({ 
     connectWith: ".sortable", 

     update: function(event, ui) { 
      position_updated = !ui.sender; //if no sender, set sortWithin flag to true 
     }, 

     stop: function(event, ui) { 
      if (position_updated) { 

       //code 

       position_updated = false; 
      } 
     }, 

     receive: function(event, ui) { 
      // code 
     } 

    }).disableSelection(); 
}); 
3

ui.sender只存在於第二個回調。

$(".sortable").sortable({ 
    connectWith: ".sortable", 
    update: function (evt, ui) { 

     // just ignore the second callback 
     if(ui.sender == null){ 

      // call ajax here 

     } 


    }, 
    receive: function (evt, ui) { 

     // called after the first 'update' 
     // and before the second 'update' 
     // ui.sender is always exists here 

    } 

}).disableSelection(); 
+0

+1優秀。這是一個令人頭疼的問題。謝謝 – 2011-10-03 05:20:06

0

只是這樣做:

update: function(event, ui) { 
      if(ui.sender) { 
      // Your actual code 
      } 
     },