2013-10-02 76 views
2

據我所知,jQuery UI Sortable只讀取一個指定的屬性來序列化或生成一個輸出數組,但我想要的是檢索多個屬性並將它們連接到一個輸出字符串中(序列化或數組)。使用jQuery檢索多個屬性Sortable

這裏是我的HTML例子:

<ul id="sort"> 
<li data-color="red" data-size="small">apple</li> 
<li data-color="yellow" data-size="medium">banana</li> 
<li data-color="green" data-size="big">watermelon</li> 
</ul> 

我需要輸出的兩個數據屬性的值。我怎樣才能做到這一點?

我試圖做這樣的事情:

$(function() { 
$('#sort').sortable().disableSelection(); 
$("#sort").on("sortupdate", function(event, ui) { 
    var output = $(this).sortable("toArray", { 
     attribute: {"data-color", "data-size"} 
    }); 
    //do something 
}); 
}); 

但它不工作,我猜是因爲attribute不支持數組作爲它的值。

回答

2

怎麼樣map()放置值的數組:

$("#sort").on("sortupdate", function(event, ui) { 
    dataArray = $.map($(this).children('li'), function(el){ 
     return {'color':$(el).data('color'), 'size':$(el).data('size')}; 
    }); 
}); 

JSFiddle