input checkbox js控制单选

本文介绍如何在HTML中使用JavaScript实现复选框(checkbox)的单选功能。通过示例代码展示了在同一div内限制多个checkbox只能选中一个的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html中checkbox的格式如下:


<div id='jfxz'>
    <div><input type="checkbox" /><span>1</span></div>
    <div><input type="checkbox" /><span>2</span></div>
    <div><input type="checkbox" /><span>3</span></div>
    <div><input type="checkbox" /><span>4</span></div>
    <div><input type="checkbox" /><span>5</span></div>
    <div><input type="checkbox" /><span>6</span></div>
</div>

function checkboxControl(){
	//$(function(){
		$("#jfxz").find(":checkbox").each(function(){
		$(this).click(function(){
			$(this).attr('checked',true);//被选中的置为true
			$("#jfxz").find(":checkbox").not($(this)).each(function(){//被选中的之外的元素,遍历置为false
				$(this).attr('checked',false);
			});
		});
		});
		//});
		
}

若checkbox仅为同一div下的多个同济checkbox,则可用:

 $(":checkbox").click(function(){
//设置当前选中checkbox的状态为checked
    $(this).attr("checked",true);
    $(this).siblings().attr("checked",false); //设置当前选中的checkbox同级(兄弟级)其他checkbox状态为未选中
});

参考:
https://blog.youkuaiyun.com/siyi_blog/article/details/80905904

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值