JQuery 操作checkbox

获取checkbox选中的状态

deleteAll全选的name

1. $("input[name='deleteAll']").is(":checked")

2. $("input[name='deleteAll']").prop("checked")

3. deleteAll.checked


设置选着的状态

$("input[name='delete']").prop("checked", false);


例子:全选选中,子全选中;全选不选,子全不选。子全选中,全选选中;子部分选中,全选不选。


$("input[name='deleteAll']").click(function(){
	if ( $("input[name='deleteAll']").prop("checked") ) {
		$("input[name='delete']").prop("checked", true);
	} else {
		$("input[name='delete']").prop("checked", false);
	}
});

$("input[name='delete']").click(function () {
	if (this.checked) {
		var all = $("input[name='delete']").length;
		var sub = $("input[name='delete']:checked").length;
		if (all === sub) {
			$("input[type='checkbox']").prop("checked", true);
		}
	} else {
		$("input[name='deleteAll']").prop("checked", false);
	}
});


<s:checkbox name="deleteAll" value="false"></s:checkbox>

<s:checkbox name="delete" value="#userItem.selected"></s:checkbox>
<s:checkbox name="delete" value="#userItem.selected"></s:checkbox>
<s:checkbox name="delete" value="#userItem.selected"></s:checkbox>
<s:checkbox name="delete" value="#userItem.selected"></s:checkbox>
<s:checkbox name="delete" value="#userItem.selected"></s:checkbox>



posted @ 2017-10-09 18:30  翠微  阅读(189)  评论(0编辑  收藏  举报