Newbie here. I am building an app in rails and I'm trying to use jquery to capture the values of multiple select boxes within a form. I've tried to simplify this down as much as possible.
form html:
<form action="/units" data-remote="true" id="add_form" method="post">
<select class="select_box" id="box_1">
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
</select>
<select class="select_box" id = "box_2"
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" value="3">3</option>
</select>
*... additional select boxes...*
<input disable_with="Loading..." name="commit" type="submit" value="Submit" />
</form>
In my application.js I want to capture the selected value of each select box.
I've tried using serialize() but can't get it to work since the form is dynamically generated using javascript (i.e. is not part of the DOM when the page is initially loaded).
Since there a lot of form elements (~20) I want to avoid working through the form elements one-by-by to capture the data, e.g.
var val_1 = $("#box_1").find(":selected").val()
var val_2 = $("#box_2").find(":selected").val()
...
Is there some way I can capture all of the selected values of a given class of elements (i.e. .select_box) and store them in an array?
perhaps something along the lines of:
var form_data = $(".select_box").find(":selected").data()
or somehow loop through the elements of class .select_box ?
Just can't quite get my head around this one. Thanks!