2010-09-24 56 views

回答

2

您正在描述的行为是使用标准HTML单选按钮完成的。如果你改变你的设计中使用这些,你会得到的好处,

  1. 用户只能选择一个项目,没有多余的JavaScript需要
  2. 用户希望能够选择多个复选框,但只有一个你正在与他们的期望

如果你仍然确定你想使用jQuery那么这样的事情应该这样做。

$(':checkbox').change(function(){ 
    if($(this).attr('checked')) { 
     $(this).siblings(':checkbox').attr('checked',false); 
    } 
}); 
+0

是的,我想用JavaScript或jQuery来做到这一点。 – vinit 2010-09-24 10:26:57

+0

不能使用单选按钮,因为我正在做类似于用户可以为两个复选框之一选择全部选项,或者他可以选择单个复选框,但他不能同时选择这两个选项。它的目的是发送邮件或短信结束列表中的用户。 – vinit 2010-09-24 10:29:24

+0

使用正确的工具进行工作+1。 – 2010-09-24 10:43:47

0

感谢您的回复。也问了我的一个朋友,他给了我以下解决方案,工作正常。张贴,如果有人需要it.-

说UR在2个clumns复选框被命名为EmailCheckBox和SMSCheckBox

然后使用此代码来切换复选框的每个单排:

$('input:checkbox[id*=EmailCheckBox]').click(uncheckOthercheckbox); 
$('input:checkbox[id*=SMSCheckBox]').click(uncheckOthercheckbox); 


     function uncheckOthercheckbox() { 

      if (this.id.indexOf("EmailCheckBox") != -1) { 

       var otherCheckBoxId = this.id.substring(0, this.id.indexOf("EmailCheckBox")) + "SMSCheckBox"; 
      } 
      else { 

       var otherCheckBoxId = this.id.substring(0, this.id.indexOf("SMSCheckBox")) + "EmailCheckBox"; 
      } 

      var i = "#" + otherCheckBoxId; 
      if (this.checked) { 

       $(i).removeAttr('checked'); 
      }     

     } 
0

@ VINIT, 只是一个小变化,你忘了其他的部分,

$('input:checkbox[id*=EmailCheckBox]').click(uncheckOthercheckbox); 
$('input:checkbox[id*=SMSCheckBox]').click(uncheckOthercheckbox); 

     function uncheckOthercheckbox() { 

      if (this.id.indexOf("EmailCheckBox") != -1) { 

       var otherCheckBoxId = this.id.substring(0, this.id.indexOf("EmailCheckBox")) + "SMSCheckBox"; 
      } 
      else { 

       var otherCheckBoxId = this.id.substring(0, this.id.indexOf("SMSCheckBox")) + "EmailCheckBox"; 
      } 

      var i = "#" + otherCheckBoxId; 
      if (this.checked) { 

       $(i).removeAttr('checked'); 
      } 
      else { 

       if ($(i).attr('checked') === false) { 
        $(i).attr('checked', 'checked'); 
       } 
      } 

     } 
相关问题