读取值判断checkbox是否需够上及全选(备忘)

本文介绍了一种使用jQuery实现根据指定值批量选中HTML页面上复选框的方法。通过解析字符串并利用jQuery的选择器和each循环,可以高效地为多个复选框设置选中状态,适用于列表项的选择操作。

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

 <html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="jquery-1.4.4.min.js"></script>          
 <script type="text/javascript">    
	$(function(){
		var str = "2,5,c";
		checked(str);
	});

	function checked(str){
		if(str.indexOf(",")==-1){
			checkValues(str);
			return;
		}
		
		var values = str.split(",");
		//遍历数组
		$.each(values,function(n,value){
			//alert(n+", "+value);
			checkValues(value);
		});
	
	}

	function checkValues(value){
		$("input:checkbox[name='pageAdapterEntity.groupIds']").each(function(){
			//$(this).attr("checked",true);
			//alert($(this).val());
				if($(this).val()==value){
					$(this).attr("checked",true);
				}
			});
	}

	//全选,反选
	function checkAll(t){
		$("input:checkbox").each(function(){
			$(this).attr("checked",t.checked);
		});
	}
   // we will add our javascript code here                                     
 </script>                                                               
 </head>                                                                 
 <body>                                                                  
   <!-- we will add our HTML content here -->      
   <table>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="1"/>1</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="2"/>2</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="3"/>3</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="4"/>4</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="5"/>5</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.groupIds" value="6"/>6</td></tr>
   <table/>

    <table>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="a"/>a</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="b"/>b</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="c"/>c</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="d"/>d</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="e"/>e</td></tr>
		<tr><td><input type="checkbox" name="pageAdapterEntity.uaids" value="f"/>f</td></tr>
   <table/>
	checkall<input type="checkbox" name="checkall"  onclick="checkAll(this)"/>
 </body>                                                                 
 </html>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值