使复选框(如单选按钮)取消选中可用



我需要有 2 个复选框作为单选按钮,但我不能使用单选按钮,因为我想取消选中可用

<script type="text/javascript">
$(document).ready(function() {
$(".gps-garmin").click(function() {
  selectedBox = this.id;
  $(".gps-garmin").each(function() {
    if ( this.id == selectedBox )
    {
      this.checked = true;
    }
    else
    {
      this.checked = false;
    };
  });
 });
});

<td><input type="checkbox" id="garmin" value="garmin" class="gps-garmin"></td>
<td><input type="checkbox" id="gps" value="gps" class="gps-garmin"></td>

实际上我的复选框的工作方式与单选按钮完全相同,我在这里找到了此代码,但我需要取消选中

试试这个:

$(".gps-garmin").click(function() {
  var check = $(this).prop('checked');
  $(".gps-garmin").prop('checked',false);
  $(this).prop('checked', (check) ? true : false);
  });

如 How-to-uncheck-a-radiobutton 中所述,您应该能够使用纯 js 或 jQuery 清除单选按钮

例如,在使用纯 js 的情况下(从链接的答案复制(:

this.checked = false;

这是你在尝试:$(".gps-garmin"(.click(function(( {

if($(this(.prop("checked"( == true({

$(".gps-garmin"(.prop('checked',false(;

$(this).prop('checked',true);

}else if($(this(.prop("checked"( == false({

$(".gps-garmin").prop('checked',false);

        }

}(;

最新更新