您的位置:首页 > Web前端 > JQuery

jquery 1.9版本下复选框 全选/取消实现

2014-07-23 17:28 543 查看
http://zhangzhaoaaa.iteye.com/blog/1914497

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript" src='jquery.js'>

</script>
<script type="text/javascript">
$(function(){
$('#chk_all').click(function(){

//1.第一种方式,以下两行代码
//var checkedOfAll=$("#chk_all").prop("checked");
//$("input[name='chk_list']").prop("checked", checkedOfAll);
//2.第二种方式,以下代码,官方代码
$("input[name='chk_list']").prop("checked", function( i, val ) {
return !val;
});
});
});
</script>
</head>

<body>
<input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
<input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
<input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
<input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
<input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
</body>
</html>


  

prop(name|properties|key,value|fn) 返回值:jQuery

概述:

获取在匹配的元素集中的第一个元素的属性值。

随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误

参数

name String V1.6

属性名称

properties Map V1.6

作为属性的“名/值对”对象

key,value String,Object V1.6

属性名称,属性值

key,function(index, attr) String,Function V1.6

1:属性名称。

2:返回属性值的函数,第一个参数为当前元素的索引值,第二个参数为原先的属性值。

示例

参数name 描述:

选中复选框为true,没选中为false

jQuery 代码:
$("input[type='checkbox']").prop("checked");

参数properties 描述:

禁用页面上的所有复选框。

jQuery 代码:
$("input[type='checkbox']").prop({
disabled: true
});

参数key,value 描述:

禁用和选中所有页面上的复选框。

jQuery 代码:
$("input[type='checkbox']").prop("disabled", false);
$("input[type='checkbox']").prop("checked", true);

参数key,回调函数 描述:

通过函数来设置所有页面上的复选框被选中。

jQuery 代码:
$("input[type='checkbox']").prop("checked", function( i, val ) {
return !val;
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: