jquery - Submit checkboxes via ajax without a form in Rails -
How can I submit checkbox values to a Rail 3 controller via AJAX (in a post Without wrapping)? In other words, how can I serialize an array's checkbox with no form.
Any help with this would be greatly appreciated thanks!
Here I am trying to do specially:
See:
& lt; Ul & gt; & Lt;% = @ item. Items | & Lt; Li & gt; & Lt;% = checkbox_tag_tag "item_ids []", item.id, false ,: class = & gt; "Item-checkbox"%> & Lt;% = item.name% & gt; & Lt; / Li & gt; & Lt;% end% & gt; & Lt; / Ul & gt;
Controller:
Class item controller & lt; ApplicationController response_to: html ,: js ,: json. . Parameter [: item_ids]} End End
javascript: def update_multiple_items item.update_all ({: category_id = & gt; params [: category_id]}, {: id = & Gt;
$ ("# update-link"). Click (function () {var categoryId = 2; var itemsArray = $ (". -Image-checkbox: checked") SerializeArray (); $ .exax ({type: "put", url: "item / update_multi_tyme", data: {category_id: range id, item_ides: items here}, datatype: "script"})}});
I get an active record error with the following information:
! Rub Y / hash: ActiveSupport :: hashwindindexensenamename: item_ids [] value: '' 303 parameter: {"parent_id" => 7 "," item_ids "=> {/ code> here The parameters are:
"0" => {"name" => "item_ids []", "value" =>, "302"}, "1" => ; {"Name" = & gt; "item_ids []", "value" => "303"}}}
I can not say definitely, but it seems that there might be a problem with your $ (".-Item-checkbox: checked "). SerializeArray ();
.. This item does not produce a simple array of ID, instead it produces an array of objects representing each checkbox.
Whether the data format looks like you want to submit it:
{"parent_id" => 7 "," item_ads "= 1 [1, 2,3]}
What you are submitting:
{"parent_id" = & gt; "7", "item_ids" = & Gt; {"0" = & gt; {"name" => "item_id []", "value" => gt; "302"}, "1" => {"name" = & gt; In other words, $ (". Item-checkbox: check"); "item_ids []", "value" => "303"}}
SerializeArray (); forms an array of palms in the format: [{name: "item_ids []", value: "302"}, {name: "item_ids []", "value" "303"}]
But you can simply [302,303]
or
What will I do (using underscore JS example):
var serializedArray = $ ( "Input: checked"). SerializeArray (); Var MadidesArere = _.map (serialized, function (item) {return item ["value"];}); # item edges should now be ["302", "303"] You can check by console.log console.log (itemIdsArray);
Then submit this array to your AJAX:
$ .ajax ({type: "put", url: "item / update_multi_item", Data: {category_ID: range_d, item_id: itemdaysArere}, datatype: "script"}); If you do not use underscores, here's how you can write it. (However, I suggest that you check it, it is very good: it is easy to use many basic functions, people who constantly do their own roles). var serializedArray = $ ("Input: check") serializeArray () ;. Var Madidas Aurerere = []; (Var i = 0, length = a.length; i & lt; length; i ++) {itemIdsArray.push (sorted array [i] ['value']); }
Comments
Post a Comment