Reputation: 4434
I would like to know how to use jQuery to save the names of selected options from a Django based multiple checkbox? It seems like I was not be able to select the group of checkbox... Can someone give me suggestions on my code? Thanks!
jsfiddle example
HTML code
<table class="tab_model">
<tbody>
<tr>
<th>
<label for="id_model_0">Model:</label>
</th>
<td>
<ul>
<li>
<label for="id_model_0">
<input type="checkbox" name="model" value="A" id="id_model_0">Model A</label>
</li>
<li>
<label for="id_model_1">
<input type="checkbox" name="model" value="B" id="id_model_1">Model B</label>
</li>
<li>
<label for="id_model_2">
<input type="checkbox" name="model" value="C" id="id_model_2">Model C</label>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<input class="submit" type="submit" value="Submit">
jQuery
var allVals = [];
$('.submit').click(function () {
$('input[id^="id_model_"] :checked').each(function () {
allVals.push($(this).val());
});
alert(allVals);
});
Upvotes: 0
Views: 440
Reputation: 3734
I would give those boxes a class, something like class='the_checkboxes'
Then use:
$(".the_checkboxes:checkbox:checked").each(function()({
allVals.push($(this).val());
});
It will be cleaner (to me, anyway).
Upvotes: 1