[ACCEPTED]-How can I iterate through all my checked checkboxes?-jquery
$("input[type=submit]").click(function () {
var answer = $("#SelectedAnswer").val();
$("input:checked").each(function () {
var id = $(this).attr("id");
alert("Do something for: " + id + ", " + answer);
});
});
0
$('input[type=checkbox]:checked').each(function() {
// Do something interesting
});
0
You'll need to add a class to your checkboxes. After 6 that, use jQuery's .each()
method like this:
HTML
<input type="checkbox" class="list">
<input type="checkbox" class="list">
<input type="checkbox" class="list">
<input type="checkbox" class="list">
<input type="checkbox" class="list">
jQuery
$(document).ready(function()
{
$("form input[type='submit']").click(function(e)
{
e.preventDefault();
// This gets the value of the currently selected option
var value = $(this).children(":selected").val();
$(".list").each(function()
{
if($(this).is(':checked'))
{
$(this).fadeOut();
// Do stuff with checked box
}
else
{
// Checkbox isn't checked
}
})
});
});
EDIT
Please 5 see this fiddle (code above). I've only implemented 4 the delete option (not very well), but use 3 the value
variable to check which option is being 2 selected. If you want this to happen before 1 the form is submitted, remove the e.preventDefault()
line.
Definitely.
The first thing you want to do 8 is bind an event handler to your submit 7 event on the form using the $.submit()
handler. Next 6 you'll want to iterate over each of the 5 checked input elements:
$('form').submit(function() {
var action = $('#SelectedAction option:selected', this).val();
$('table input:checkbox:checked').each(function(i){
return doMyCustomFunction(this, action);
});
}
Your custom function 4 ("doMyCustomFunction()
" in my example) should return either 3 true or false depending on whether you wanted 2 to submit your form or not.
By way of a practical 1 example, it might look something like this:
function doMyCustomFunction(el, formAction) {
console.info('The element ID is ' + el.id + ' and the action is ' + formAction);
return true;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.