2012-09-02 28 views
1

jQuery代碼如何獲得可排序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"); 

,但不能得到它的工作,請大家幫我

謝謝

+0

你能粘貼你的更新代碼嗎?你得到了什麼結果? –

回答

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 

如果只想存儲一個數據類型的每一行,然後你可以只使用一個陣列,並推到它的循環。

相關問題