如何在单击其他复选框时从复选框中删除检查属性

时间:2011-12-15 08:34:55

标签: jquery html

我有两个checboxes

<input type="checkbox" name="some" >
<input type="checkbox" name="other" >

当我检查其中一个时,我想要做的事情是其他人应该自动取消选中。请记住,两个复选框的名称更改不相同。请帮我 。 Thanx提前

5 个答案:

答案 0 :(得分:1)

$('input[name="some"]').click(function(){
    $('input[name="other"]').removeAttr('checked');
});

$('input[name="other"]').click(function(){
    $('input[name="some"]').removeAttr('checked');
});

工作样本

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<input type="checkbox" name="some" >
<input type="checkbox" name="other" >

<script>
$('input[name="some"]').click(function(){
    $('input[name="other"]').removeAttr('checked');
});

$('input[name="other"]').click(function(){
    $('input[name="some"]').removeAttr('checked');
});

</script>

答案 1 :(得分:0)

尝试使用单选按钮,因为您不必添加任何javascript代码!
单选按钮只允许一个选择。

答案 2 :(得分:0)

我认为在复选框上使用类并从那里调用函数是一个不错的选择。

答案 3 :(得分:0)

如果您使用的是jQuery v1.6或更高版本;

$('input[name="some"]').click(function(){
    $('input[name="other"]').prop("checked", false);
});

当点击“some”复选框时,此代码会取消选中“其他”复选框。

答案 4 :(得分:0)

$("input[type=checkbox]").click(function(){

  $('input[type=checkbox]').each(function () {
      $(this).prop("checked", false);
  }
   $(this).prop("checked", true);

});
相关问题