JQuery可排序序列化

时间:2014-06-21 10:52:33

标签: javascript jquery html ajax jquery-ui

我正在使用http://johnny.github.io/jquery-sortable/对表进行排序,我让JQuery工作没有问题,但我无法使序列化正常工作。我想知道是否有人可以提供帮助并解释我需要做什么?

所以我的HTML是。

<table class="table table-striped table-bordered sorted_table" id="positionstable">
    <tbody>
        <tr>
            <th class="sectionname" colspan="1">Position</th>
        </tr>
        <tr data-id="2">
            <td class=" ">Item 2</td>
        </tr>
        <tr data-id="1">
            <td class=" ">Item 1</td>
        </tr>
        <tr data-id="3">
            <td class=" ">Item 3</td>
        </tr>
        <tr data-id="4">
            <td class=" ">Item 4</td>
        </tr>
    </tbody>
</table>

我的JQuery是:

$('.sorted_table').sortable({
  containerSelector: 'table',
  itemPath: '> tbody',
  itemSelector: 'tr',
  placeholder: '<tr class="placeholder"/>',

  onDrop: function (item, container, _super) {
            var dataToSend = $('.sorted_table').sortable("serialize").get();

            console.log(dataToSend)

            $.ajax({
                url: "ajax_action.php",
                type: "post",
                data: dataToSend,
                cache: false,
                dataType: "json",
                success: function () {}
            });
            //_super(item, container);
        }
})

您还可以在http://jsfiddle.net/WJProctor/7GqQu/

找到代码

我想将一系列ID发送到我的ajax脚本。

我期待你的任何帮助和建议。

亲切的问候

詹姆斯

1 个答案:

答案 0 :(得分:1)

序列化通常用于表单元素并生成JSON对象,使用每个字段的名称作为键,元素的值作为其值。

您可以轻松生成序列化对象,就像循环遍历表格行一样

$('.sorted_table').sortable({
  containerSelector: 'table',
  itemPath: '> tbody',
  itemSelector: 'tr',
  placeholder: '<tr class="placeholder"/>',

  onDrop: function (item, container, _super) {
            var obj = jQuery('.sorted_table tr').map(function(){
                return 'trvalue[]=' + jQuery (this).attr("data-id");
            }).get();
          console.log(obj)
          //do the ajax call
        }
})

http://jsfiddle.net/7GqQu/1/

相关问题