繁体   English   中英

如何获取表头中选中的单选按钮的位置

[英]How to get the position of checked radio button in table header

我想获得已检查表标题的位置,而不使用这些单选按钮的唯一ID或值。 我怎样才能做到这一点?

 $("#save").on('click', (function() { alert(); })); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" id="save">Save</button> <table border=1> <tr> <th> <input type="radio" name="header" /> <br /> A </th> <th> <input type="radio" name="header" /> <br /> B </th> <th> <input type="radio" name="header" /> <br /> C </th> </tr> <tr> <td>row 1 </td> <td>row 1 </td> <td>row 1 </td> </tr> </table> 

假设'position'是指包含已检查的无线电输入的th元素的索引,您可以通过使用:has(input:checked) ,然后获取其index()来找到它,如下所示:

 $("#save").on('click', (function() { var index = $('th:has(input:checked)').index(); console.log(index); })); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" id="save">Save</button> <table border=1> <tr> <th> <input type="radio" name="header" /> <br /> A </th> <th> <input type="radio" name="header" /> <br /> B </th> <th> <input type="radio" name="header" /> <br /> C </th> </tr> <tr> <td>row 1 </td> <td>row 1 </td> <td>row 1 </td> </tr> </table> 

暂无
暂无

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

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