How to get checkbox check value of specified div in jQuery

<td id="optrep0"> <input type="checkbox" value="1" class="warna" id="warna" name="warna[]"> <input type="checkbox" value="2" class="warna" id="warna" name="warna[]"> </td> <td id="optrep1"> <input type="checkbox" value="1" class="warna" id="warna" name="warna[]"> <input type="checkbox" value="2" class="warna" id="warna" name="warna[]"> </td> <td id="optrep2"> <input type="checkbox" value="1" class="warna" id="warna" name="warna[]"> <input type="checkbox" value="2" class="warna" id="warna" name="warna[]"> </td> 

I need to find the checked value of the specified div with id s: optrep0 , optrep1 , optrep2 above, I tried using

 var optrep0= jQuery(':checkbox:checked').map(function () { return this.value; }).get(); 

And send the optrep0 variable to the server, but it will send each checked value. So, I want to send only the specified div just for the variable, I also tried

  var optrep0= jQuery('#optrep0>#warna:checkbox:checked').map(function () { return this.value; }).get(); 

PS: the subdirectory name on td id cannot be changed as is, I only need a javascript example, how to solve this case, thanks: D

+8
javascript jquery html css php
source share
3 answers

Set a space in > and use dot for class warna

Live demo

 var optrep0= jQuery('#optrep0 > .warna:checkbox:checked').map(function () { return this.value; }).get(); 
+6
source share

Try using the child selector by placing > and do not use :checkbox , only :checked

 var optrep0= jQuery('#optrep0 > :checked').map(function () { return this.value; }).get(); 
+3
source share

I would say the preferred way

 var optrep0= jQuery('#optrep0 > .warna:checkbox:checked').map(function () { return this.value; }).get(); 

Demo

0
source share

All Articles