我想获取已检查表标题的位置,而不使用这些单选按钮的唯一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>
&#13;
答案 0 :(得分:3)
假设'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>