我有不同级别的单选按钮组,每个级别都有相同的类:
<div> <!-- radio group one -->
<input type="radio" name="rdo1" class="some1 text4 radio_group_1" />
<input type="radio" name="rdo2" class="some2 text5 radio_group_1" />
<input type="radio" name="rdo3" class="some3 text6 radio_group_1" />
</div>
<div> <!-- radio group two -->
<input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />
<input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />
<input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />
</div>
... <!-- radio group three (and so on) -->
由于服务器限制,我无法更改单选名称。因此,我唯一拥有的是以'
radio_group_
'开头的类。我使用this solution和this失败,因为类名位于其他类名的中间。如何选择具有相同班级名称的收音机并使它们像组收音机一样工作?
最佳答案
您是否想要这样的行为:
$(document).ready(function() {
$("div > input[type=radio]").click(function() {
var thisParent = $(this).closest("div");
var prevClicked = thisParent.find(":checked");
var currentObj = $(this);
prevClicked.each(function() {
if (!$(currentObj).is($(this))) {
$(this).prop("checked", false);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Group 1
<div>
<!-- radio group one -->
<input type="radio" name="rdo1" class="some1 text4 radio_group_1" />rdo1
<input type="radio" name="rdo2" class="some2 text5 radio_group_1" />rdo2
<input type="radio" name="rdo3" class="some3 text6 radio_group_1" />rdo3
</div>
<br/>Group 2
<div>
<!-- radio group two -->
<input type="radio" name="rdo4" class="some1 text4 radio_group_2 other1" />rdo4
<input type="radio" name="rdo5" class="some2 text5 radio_group_2 other2" />rdo5
<input type="radio" name="rdo6" class="some3 text6 radio_group_2 other3" />rdo6
</div>