《JQuery》实现同一个复选框checkbox一键全选和一键取消

给全选复选框添加点击事件

HTML代码:

<input  type="checkbox"  id="ckall" onclick="checkall()"/>全选
<input  type="checkbox" class="ckitem" />学习
<input  type="checkbox" class="ckitem" />吃饭
<input  type="checkbox" class="ckitem" />打游戏
<input  type="checkbox" class="ckitem" />运动

JS代码:

function checkall(){
		// 判断全选按钮是否被选中checked
		if($("#ckall").is(":checked") == true){
			$(".ckitem").each(function(){
				$(this).prop("checked",true);	// 通过prop()函数设置复选框的选中状态
			})
		}
		else{
			$(".ckitem").each(function(){//each是循环遍历
			$(this).prop("checked",false);
		  })
		}
}

猜你喜欢

转载自blog.csdn.net/weixin_45947938/article/details/124738353