I’m confused regarding each() and .each()… I think I need to use both, but I’m not sure how…
Basically, I have a series of checkboxes and when they’re checked, I want to get the id of the closest table row and add it to an array that I can then serialize and pass to a function.
So, the html looks like this:
<tr id="t1"><td><input type="checkbox" name="did[]" value="232" class="bd" /></td></tr>
<tr id="t2"><td><input type="checkbox" name="did[]" value="234" class="bd" /></td></tr>
<tr id="t3"><td><input type="checkbox" name="did[]" value="676" class="bd" /></td></tr>
<tr><td><button id="tsid" name="tsid" type="button">Send</button></td></tr>
And, when (and if) the checkbox is selected, I want to get the closest tr id and store it in an array:
var trids = []
And then serialize the input data and the trids array…
So far, I only have this:
$('#tsid').click(function() {
//meant to get the values of the selected checkboxes
var myData = $('input[name=did[]]').serialize();
//meant to find and store the closest tr ids in an array...
$('.bd').each(function() {
var trids = $(this).closest("tr").get(0); //and now I'm lost...
});
}
It should just be:
$('.bd:checked')selects only the checked checkboxes.closest('tr')gets the parenttras you already did it correctly.attr('id')gets theidof thistr. There is no need inget(0)asclosest()already selects just one element.Then this value is added to the array
tridsby using thepush()method on the array.Update:
jQuery’s
.searialize()function generates a string out of the values of form elements, e.g.:I don’t think it works on arrays though. The only solution I know would be generate the string for the array by yourself:
This would append the array values to the serialized string.
But I guess there is a better solution for this.