使用jQuery取消选中所有复选框,然后选中单击的复选框

使用jQuery取消选中所有复选框,然后选中单击的复选框,jquery,Jquery,我有以下资料: <div class="pricing-levels-2"> <p>Which level would you like? (Click all that apply)</p> <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 1<br> <in

我有以下资料:

      <div class="pricing-levels-2">
        <p>Which level would you like? (Click all that apply)</p>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 1<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 2<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 3<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 4<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 5<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 6<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 7<br>
      </div>
      <div class="pricing-levels-3">
        <p>Which level would you like? (Click all that apply)</p>
        <input class="single-checkbox"type="checkbox" name="vehicle" value="Bike">Level 1<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 2<br>
        <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 3<br>
      </div>
但问题是,现在所有的复选框都清除了,即使是我正在选择的复选框。所以我不能选择任何一个(我想选择我正在点击的那个)

我做错了什么?

您需要改用:

您需要改为使用:

参考文献

参考文献

jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
  jQuery('.pricing-levels-2 .single-checkbox').attr('checked', false);
  jQuery(this).attr('checked', true);
});
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
  jQuery('.pricing-levels-2 .single-checkbox').prop('checked', false);
  jQuery(this).prop('checked', true);
});
   jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
      jQuery('.pricing-levels-2 .single-checkbox').removeAttr('checked');
      jQuery(this).attr('checked', 'checked');
    });
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
  jQuery('.pricing-levels-2 .single-checkbox').prop('checked', false);
  jQuery(this).prop('checked', true);
});