繁体   English   中英

我如何才能从同一个复选框中获取隐藏值 <td> 标签

[英]How can I get hidden value by checkbox value from under the same <td> tag

我有一张这样的桌子:

<table id="TempTable">
 <tr>
  <td>
   <input type="checkbox" id="chkbox1"/>
   <input type="hidden" value="1" />
  </td>
 </tr>
 <tr>
  <td>
   <input type="checkbox" id="chkbox2"/>
   <input type="hidden" value="2" />
  </td>
 </tr>
 <tr>
  <td>
   <input type="checkbox" id="chkbox3"/>
   <input type="hidden" value="3" />
  </td>
 </tr>
</table>

我想在与选中的复选框相同的td获取隐藏值。

我尝试了这个:

$("#TempTable tr input:checkbox:checked").each(function(){
  alert($("#"+this.id).parent("td").child("input:hidden").val());
});

当然,在获得所需的价值后,我会将其保存到数组中。

尝试这个:

$("input[type='checkbox']").each( function (){
    var value = $(this).parent().find("input:hidden").val();
    // this will return the value of each hidden field
});

不知道为什么这些值不在复选框中,这样可以简化代码。

但是有了您的代码,您只需要使用next和map。

 $(":checkbox").on("change", function() { var vals = $(":checkbox:checked").map( function(){ return $(this).next().val(); }).get(); console.log(vals); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="TempTable"> <tr> <td> <input type="checkbox" id="chkbox1"/> <input type="hidden" value="1" /> </td> </tr> <tr> <td> <input type="checkbox" id="chkbox2"/> <input type="hidden" value="2" /> </td> </tr> <tr> <td> <input type="checkbox" id="chkbox3"/> <input type="hidden" value="3" /> </td> </tr> </table> 

正如我在将值放在复选框之前所说的那样

 $(":checkbox").on("change", function() { var vals = $(":checkbox:checked").map( function(){ return this.value; }).get(); console.log(vals); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="TempTable"> <tr> <td> <input type="checkbox" id="chkbox1" value="1"/> </td> </tr> <tr> <td> <input type="checkbox" id="chkbox2" value="2"/> </td> </tr> <tr> <td> <input type="checkbox" id="chkbox3" value="3"/> </td> </tr> </table> 

您可以使用同级和隐藏的输入类型来获取值。

$("#TempTable tr input[type=checkbox]:checked").each(function(){
   $(this).siblings('input[type=hidden]').val()
});

暂无
暂无

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

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