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

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

  • sortable('serialize', {attribute: 'data-id'})给出一个空字符串
  • sortable('toArray'), {attribute: 'data-id'})给出预期的输出
 
1
2
3
4
5
 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. } }); 

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

这是怎么回事? 我98%肯定这是以前的工作。

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

在您的情况下,您可以设置所需的attributekeyexpression ,以便它获取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 }); 

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

如果您没有“something- n ”forms的id值,则“serialize”方法不起作用。 (您可以使用_=而不是- 。)

我们的想法是为您提供一个查询字符串,其中包含“something”作为参数名称,以及-作为值后面的值。