AJAX使用jQuery可排序(非表单)发布附加数据


AJAX posting additional data using jQuery sortable (not form)

我有一个页面,它使用jQuery UI可排序插件来使用AJAX更新项目的排序顺序。这一切都很好,但我希望能够添加使用复选框删除项目的功能。我可以将额外的静态信息传递给帖子,但我不知道如何发送动态创建和选择的项目的查询字符串。

这是我的jQuery:

var fixHelper = function(e, ui) {
  ui.children().each(function() {
    $(this).width($(this).width());
  });
  return ui;
};
$(document).ready(function(){
  $("#sortable tbody").sortable({ 
    helper: fixHelper,
    opacity: 0.6, 
    update: function(){
      $('#savemessage').html('<p>Click <em>Remove/Reorder</em> to save</p>');
      }
  });
  $('#button').click(function(event){
    var order = $("#sortable tbody").sortable("serialize");
    order += "&crudtype=update_favorites";
    $('#savemessage').html('<p>Saving changes...</p>');
    $.post("/crud",order,function(theResponse){
        $('#savemessage').html(theResponse);
      });
  });
});

这是我的PHP,它渲染表行和数据:

foreach ($favorites as $key => $favorite) {
  print '<tr class="' . zebra($key) . '" id="field_' . $favorite['fid'] . '">
    <td class="handle"><a href="#">' . $favorite['name'] . '</a></td>
    <td><input type="checkbox" name="fid[]" id="fid" value="' . $favorite['fid'] . '" class="box check-child"></td>
    </tr>';
}
print '<input type="submit" name="submit" value="Remove/Reorder Selected Favorites" class="form-submit-table" id="button">';

最终,我希望能够收集检查过的fid[]的值,然后(大概)将它们添加到JSorder变量中,该变量将传递到我的AJAX处理页面,但我不知道如何做到这一点,因为我有点像jQuery新手。

我看过一些关于如何从表单中获取信息的帖子,但在这种情况下,由于我使用的是Sortable插件,所以我实际上并没有使用表单

我不确定表单名称是什么。。但是

var str = $("form[name=yourForm").serialize();

应该这样做。

换句话说,

<form name="yourForm">
<?php /* your code */ ?>
</form>

var formString = $("form[name=yourForm").serialize();
$.ajax({
    type : 'post',
    data : formString,
    success : function(response){
        alert(response);
    }
});