1

jQuery代码

    $(function() {
$("#list .sections").sortable({ opacity: 0.5, cursor: 'move',update: function() {

        var order = $(this).sortable("serialize") + '&update=order';

                    $('#form').submit(function()
                    {
                        $.ajax
                        ({
                            type: "POST",
                            url: "update.php",
                            data: order,
                            success: function()
                            {
                            alert(success)
                            }
                        });                        
                    });

    }                                 
    });
});

HTML 和 PHP 代码

      <form action="" method="post" id="form">  
  <div id="list">
    <div class="sections">
    <div id="response"> </div>

      <?php
        include("connection.php");
        $query  = "SELECT * FROM dragdrop ORDER BY listnumber ASC";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $id = stripslashes($row['id']);
            $text = stripslashes($row['text']);
        ?>

      <div id="listOrder_<?php echo $id ?>" class="box_inner">
          <div id="section_head">
            <?php echo $text?>  
            <span class="id"><?php echo $id; ?></span>
                            <input type="hidden" name="value" value="need value" />
          </div>
          <div id="section_body">
                            <h2> Drag Me </h2>
          </div>
      </div>
      <?php } ?>

    </div>
          <input type="submit" id="save_element" value="Save element" style="margin-left: 40px" />
        </div>
      </form>

我的问题是如何通过序列化获取 listOrder_ id 下的所有值?

我尝试过

var value = $('input[name="value"]').val();

$.(value).sortable("serialize");

但不能让它工作,请帮助我

谢谢

4

1 回答 1

0

您不需要使用表单和隐藏字段。

示例标记:

<div id="container">
    <span data-name="a"></span>
    <span data-name="b"></span>
    <span data-name="c"></span>
  </div>

脚本:

<script>
var obj = {};
$('#container span').each(function(index){
    var name = $(this).data('name');
    obj[index] = [];
    obj[index] = {'name' : name};
});
</script>

$.post('update.php', {'data' : obj}); //update list

如果您只想在每一行中存储一种类型的数据,那么您可以使用一个数组并在循环中推送到它。

于 2012-09-02T06:19:13.373 回答