views:

56

answers:

2

I has checkbox the list of media in my webpage

<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaA
<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaB
<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaC

In my javascript,How can i keep the checkbox value inside the array for example

if user check for mediaA and mediaC in array will be {12,14}

Thanks ^_^

A: 

If jQuery is an option, you can get an array of the values using .map() like this:

var vals = $("input[name=c1]:checked").map(function(i, cb) {
  return $(cb).val();
}).get();
alert(vals);

vals would contain an array of the values of the checked checkboxes, e.g. [12, 14] if A and C were checked in this:

<input name="c1"  type="checkbox" value="12" /> mediaA
<input name="c1"  type="checkbox" value="13" /> mediaB
<input name="c1"  type="checkbox" value="14" /> mediaC
Nick Craver