jQuery UI 可排序不使用自定义属性序列化

Wes*_*rch 2 javascript jquery jquery-ui

我正在使用可通过data-id属性排序的 jQuery UI 。我知道你可以使用sortable('serialize')类似的东西id="row_4",这对我有用,但我需要这样做。

  • sortable('serialize', {attribute: 'data-id'}) 给出一个空字符串
  • sortable('toArray'), {attribute: 'data-id'}) 给出预期输出
<div data-sortable="link/to/handler">
    <div data-id="1">1</div>
    <div data-id="2">2</div>
    <div data-id="3">3</div>
    <div data-id="4">4</div>
    <div data-id="5">5</div>
</div>
Run Code Online (Sandbox Code Playgroud)
var container = $('[data-sortable]');
container.sortable({
    items : "> [data-id]",
    update : function() {
        var postData = container.sortable('serialize', {
            attribute: 'data-id'
        });
        alert(postData); // Nothing
        var postData2 = container.sortable('toArray', {
            attribute: 'data-id'
        });
        alert(postData2); // 1,3,4,5,2 etc.
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴:http : //jsfiddle.net/ogzw5pL2/

这是怎么回事?我 98% 确定这以前有效。

Jul*_*ire 5

你需要keyvalue为了serialize,但您可以使用参数来覆盖默认行为并获得想要的结果。

在您的情况下,您可以设置attribute,key并且expression您希望它使用data-id和 构建具有定义key和正确值的字符串。像这样:

           var postData = container.sortable('serialize', {
                attribute: 'data-id',//this will look up this attribute
                key: 'order',//this manually sets the key
                expression: /(.+)/ //expression is a RegExp allowing to determine how to split the data in key-value. In your case it's just the value

            });
Run Code Online (Sandbox Code Playgroud)

小提琴:http : //jsfiddle.net/c2o3txry/