2012-02-25 58 views
3

目前我正在使用ajax提交我的可排序項目,但我想要做一個非ajax提交。那可能嗎?是否有可能在正常的非Ajax提交中使jQuery可排序的序列化項目可用?

當前阿賈克斯後:

$("#create_items_form").submit(function() { 
    $.ajax({ 
     type: "POST", 
     url: $(this).attr("action"), 
     dataType: "script", 
     data: $("#destination_items").sortable('serialize') 
    }); 
    return false; 
}); 

HTML:

<%= form_for(@items, :url => create_items_path, :html => {:id => "create_items_form"}) do |f| %> 
    <ul id="destination_items"></ul> 
<%= f.submit "Save", :id => "create_items_button" %> 

正因爲如此,我得到一個不錯的數組在我的控制器使用方法:

Params: "items"=>["8", "10"] 

是否有可能使這個通過正常提交的信息?

謝謝!

回答

6

您可以使用提交的序列化數據填充隱藏的輸入。在這裏你不需要在事件處理程序中返回false,因爲你不想停止提交事件。

添加到您的窗體:

<input id="someid" name="yourfieldname" type="hidden"> 

綁定上提交事件:

$("#create_items_form").on('submit', function() { 
    $('someid').val($("#destination_items").sortable('serialize')); 
}); 
+0

我試過,但它的傳遞字符串而不是數組的對象。是否有可能在HTML隱藏的輸入標籤中傳遞數組? – EverTheLearner 2012-02-27 20:22:25

+1

@EverTheLearner:你可以使用這個解析該字符串到一個哈希:http://stackoverflow.com/questions/2772778/parse-string-as-if-it-were-a-querystring-in-ruby-on-rails – DCoder 2012-02-27 20:48:26

+0

@DCoder:謝謝!這有助於解析字符串! – EverTheLearner 2012-02-28 01:56:27

相關問題