我有以下内容:
<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>
我希望每次点击其中一个时.pricing-levels-2
复选框都清除,所以我做了:
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').attr('checked', false);
jQuery(this).attr('checked', true);
});
但问题是,现在所有复选框都清除了,即使是我正在选择的复选框。所以我不能选择任何(我希望我点击的那个被选中)。
我做错了什么?
答案 0 :(得分:5)
您需要使用.prop代替:
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').prop('checked', false);
jQuery(this).prop('checked', true);
});
答案 1 :(得分:1)
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);
});