简体   繁体   English

如何在jQuery中获取指定div的复选框值

[英]How to get checked checkbox 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 checked value of specified div s having id s: optrep0 , optrep1 , optrep2 above, I have tried using 我需要找到id s的指定div的检查值: optrep0optrep1optrep2以上,我试过用

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

And send optrep0 variable to server, but it will send every checked value. 并将optrep0变量发送到服务器,但它将发送每个选中的值。 So, I want to send only specified div s only per variable, I also tried 所以,我想只发送每个变量的指定div ,我也尝试过

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

PS: sub id name on td id cannot be changed as is as, I only need javascript example how to solve this case, thank you :D PS:td id上的sub id名称不能改为,我只需要javascript示例如何解决这种情况,谢谢:D

Give space in > and use dot for class warna >给出空间并使用dot作为类warna

Live Demo 现场演示

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

Try using descendant selector by putting a > and don't use :checkbox , only :checked is needed 尝试使用后代选择器,只需输入一个> ,不要使用:checkbox :仅需要:checked

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

I would say the preferable way is 我想说更好的方法是

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM