views:

48

answers:

3

I have a table that i am enumerating through my model with. I have this check box there for each row.

   <input type="checkbox" name="IsSelected" value="<%=item.PartNo%>" />

I would like to use jquery to find out which of the check boxes are checked I tried doing this

  var selected = $("#IsSelected").val();

but selected is just undefined. is there a way to do this? Thank you

+2  A: 
var checkedCheckboxes = $("input[name='IsSelected']:checked");

checkedCheckboxes should then be a collection of the checked checkboxes

Adam
ok let say I want to display in an alert which boxes are selected?using alert(selected) just shows "object"and alert(selected[0]) shows [objecteInputElement]how would I work with them to get the values of them?
twal
@twal Try $.each(checkedCheckboxes,function(k,v){ alert(v.val();)});
Adam
$.each(checkedCheckboxes,function(k,v){ alert(v.val();)}); gives me an error saying unexpected ;if I remove the ; after val(); I get an error saying Object doesn't support this property or method
twal
sorry. how about: $("input[name='IsSelected']:checked").each(function(i,e){alert($(this).val())})
Adam
Great thank you very much. I made a small change to make all of the selected values appear in one alert like this: var checkedCheckboxes = $("input[name='IsSelected']:checked"); var checkedString = ""; $("input[name='IsSelected']:checked").each(function (i, e) { checkedString += ($(this).val()) }) alert(checkedString);
twal
A: 

First of all you should change your code to

<input type="checkbox" id="IsSelected" name="IsSelected" value="<%=item.PartNo%>" />

You can use jquery .is(':checked') to determine if checkbox is checked or not.

Lukasz Dziedzia
Because the OP is enumerating a collection of items and thus outputting the same element potentially multiple times on the same page, the `id` attribute should NOT be used as you suggest. The `id` attribute must be unique in the document.
Marve
Yeah, right! I just wanted to point out that $('#IsSelected') will not work if IsSelected is name.
Lukasz Dziedzia
A: 

http://drupal.org/node/116548

Kaan