jQuery:如何select只有一台收音机具有相同的类?

jQuery: How to select only one radio with same classes?

我有不同级别的单选按钮组,每个级别具有相同的 class:

<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_”开头的 classes。我使用 this solution and this 没有成功,因为 class 名称位于其他 class 名称的中间。

如何 select 具有相同 class 名称的收音机并使它们像群组收音机一样工作?

我不知道你为什么说:

I can't change the radio names

无论如何,您所问的答案可能是这样的:

$('.radio_group_1, .radio_group_2').click(function(){
  
  $(this).prop('checked', true).siblings().prop('checked', false);
  
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

你想要这样的行为吗:

$(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>

<div id="radio_group_1">
  <!-- 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>
<div id="radio_group_2">
  <!-- 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>

$("input[type=radio]").on("click", function(){
 var groupName = $(this).closest("div").attr("id");
   $("input[type=radio]."+groupName).prop("checked", false);
   $(this).prop("checked", true);
});

https://jsfiddle.net/tr73o3e6/2/

$("input[type=radio]").click(function () {
    var thisClass = $(this).attr("class");
    console.log(thisClass);

    $('.'+thisClass).each(function () {
        $(this).prop("checked", false);
    });
    $(this).prop("checked", true);
});

你有多个 classes 所以 select class 你想传递给每个