jQuery:让复选框表现得像单选按钮? [英] jQuery: make checkboxes act like radio buttons?

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

问题描述

有没有办法让复选框表现得像单选按钮?我认为这可以用 jQuery 来完成?

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.

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

UPDATE 添加了分组分离.

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

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