我希望能够单击一个标签,并查看它属于哪个单选按钮组。我试过这个,但它总是在 li (name=left) 之后找到第一个输入,而不是最接近标签的那个。我该如何解决?
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $(this).closest('ul').find('input[type=radio]:enabled').attr('name');
alert("group name: " + thisGroupName);
});
HTML
<ul>
<li>
<input type="radio" class="radio-red" name="left" id="a1">
<label for="a1">a1</label>
<input type="radio" class="radio-blue" name="right" id="a2">
<label for="a2">a2</label>
</li>
<li>
<input type="radio" class="radio-red" name="left" id="b1">
<label for="b1">b1</label>
<input type="radio" class="radio-blue" name="right" id="b2">
<label for="b2">b2</label>
</li>
<li>
<input type="radio" class="radio-red" name="left" id="b3">
<label for="b3">b3</label>
<input type="radio" class="radio-blue" name="right" id="b4">
<label for="b4">b4</label>
</li>
</ul>