jQuery:使复选框像单选按钮? [英] jQuery: make checkboxes act like radio buttons?

查看:120
本文介绍了jQuery:使复选框像单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使复选框的行为像单选按钮?我假设这可以用jQuery完成?

 < input type =checkboxclass =radiovalue =1 name =fooby [1] []/> 
< input type =checkboxclass =radiovalue =1name =fooby [1] []/>
< input type =checkboxclass =radiovalue =1name =fooby [1] []/>

< input type =checkboxclass =radiovalue =1name =fooby [2] []/>
< input type =checkboxclass =radiovalue =1name =fooby [2] []/>
< input type =checkboxclass =radiovalue =1name =fooby [2] []/>

如果选中一个复选框,群组中的其他人将取消选中。

解决方案

  $(input:checkbox)click(function {){
var group =input :checkbox [name ='+ $(this).attr(name)+'];
$(group).attr(checked,false);
$ ).attr(checked,true);
});

这样做,虽然我同意这可能是一个坏主意。



在线示例: http://jsfiddle.net/m5EuS/1/ p>

UPDATE 添加群组分隔。


Is there a way to make checkboxes act like radio buttons? I assume this could be done with jQuery?

<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />

<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />

If one box was checked the others in the group would uncheck.

解决方案

$("input:checkbox").click(function(){
    var group = "input:checkbox[name='"+$(this).attr("name")+"']";
    $(group).attr("checked",false);
    $(this).attr("checked",true);
});

This will do it, although i do agree this might be a bad idea.

Online example: http://jsfiddle.net/m5EuS/1/

UPDATE added group separation.

这篇关于jQuery:使复选框像单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆