2011-05-05 65 views
1

真的需要幫助jquery可排序提醒此列表ID

我正在使用jQuery排序。

而我正在尋找只是從被拖動的列表元素中獲取id。

「不是全部關閉它們」

下面是一個例子http://jsfiddle.net/isimpledesign/85LdV/1/

此提醒回一個數組,但我需要它給我帶回那個被拖動的元素只是id,這樣我就可以通過它到一個PHP文件。

有人可以幫助我嗎?

回答

3

只是爲了澄清乍得的回答有點 -

$(function() { 
    $("#sortable").sortable({ 
     update: function(event, ui) { 
      // i need to get the class text that is being dragged i.e 
      var order = $(this).sortable("serialize"); 
      alert(order); 
      /* 
      No need to bind any other events, ui.item is the dragged 
      item in 'update' too and we only want to capture the id when the sort 
      has changed presumably 
      */ 
      alert(ui.item.attr('id')); 
      /* 
      No need for subscripting, ui.item is a jquery object so 
      we can just call attr() on it to get the ID 
      */ 
     } 
    }); 
}); 
+0

+1哇,我甚至懶得檢查更新事件的UI參數,這對我來說是愚蠢的。好決定。 – Chad 2011-05-05 14:47:59

+0

不用擔心,它仍然是在一天的早期:) – HurnsMobile 2011-05-05 14:48:35

+0

非常感謝這麼多;) – DCHP 2011-05-05 14:57:47

0

使用start事件:

$(function() { 
    $("#sortable").sortable({ 
     update: function(event, ui) { 
     // i need to get the class text that is being dragged i.e 
     var order = $(this).sortable("serialize"); 
     alert(order); 
     }, 
     //Start event fires on the start of a sort 
     //you can store the id from in here 
     start: function(event, ui) { 
     //here ui.item contains a jquery object that is the item being dragged 
     alert(ui.item[0].id); 
     } 
    }); 
}); 
+0

我猜是更好地使用「更新」,而不是「開始」 – Luccas 2011-05-05 14:38:47

+0

@盧卡斯坦率地說,我不確定你的意思,他們是不同的事件,在不同的時間發射,並通過不同的信息。 [jQuery.sortable](http://jqueryui.com/demos/sortable/) – Chad 2011-05-05 14:43:42

0

使用此:

$(function() { 
    var lastMoved = null; 
    $("#sortable li").mousedown(function(){ 
     lastMoved = this; 
    }); 
    $("#sortable").sortable({ 
     update: function(event, ui) { 
     alert($(lastMoved).attr("id")); 
     } 
    }); 
}); 

它的測試工作。 希望這有助於。乾杯。

+0

這是不可靠的,因爲它不依賴於任何排序。即使排序沒有改變,只要有一個對象被點擊了,你就會返回ID。 – HurnsMobile 2011-05-05 14:47:27

+0

@HurnsMobile。不你錯了。它顯示'update'方法中的值,僅當您更改順序時纔會發生。請分析它並重新考慮你的downvote。 – 2011-05-05 14:47:38

+0

看到我上面的評論 – HurnsMobile 2011-05-05 14:49:01