jQuery 的全选(全非选)即取得被选中的值使用介绍
更新时间:2013年11月12日 17:22:50 作者:
全选、全非选即取得被选中的值在日常开发应用中很广泛,下面有个不错的示例,大家可以感受下
页面代码
<body>
<div>
<input id="checkall" type="checkbox" value="天" />全部<br/>
<div id="con">
<input type="checkbox" name="checkbox" value="天" />天
<input type="checkbox" name="checkbox" value="空" />空
<input type="checkbox" name="checkbox" value="飞" />飞
<input type="checkbox" name="checkbox" value="来" />来
<input type="checkbox" name="checkbox" value="五" />五
<input type="checkbox" name="checkbox" value="个" />个
<input type="checkbox" name="checkbox" value="字" />字
<input type="checkbox" name="checkbox" value="这" />这
<input type="checkbox" name="checkbox" value="都" />都
<input type="checkbox" name="checkbox" value="不" />不
<input type="checkbox" name="checkbox" value="算" />算
<input type="checkbox" name="checkbox" value="事" />事</div>
</div>
<script src="Scripts/jquery-1.4.1.js" language="javascript"></script>
<script src="JqueryAll.js" language="javascript"></script>
</body>
js中的代码
/*js部分
* 绑定批量选定/非选
* 例子
*/
$(function () {
$("#checkall").click(function () {
var stu = $(this).attr("checked");
checkAll(stu);
})
$("input[name='checkbox']").click(function () {
uncheckAll();
})
})
function checkAll(status) {
$("input[name='checkbox']").each(function () {
$(this).attr("checked");
$(this).attr("checked", status);
})
}
function uncheckAll() {
alert($("input[name='checkbox']:checked").length);
if ($("input[name='checkbox']:checked").length == $("input[name='checkbox']").length) {
$("#checkall").attr("checked",true);
}
else {
$("#checkall").attr("checked", false);
}
}
复制代码 代码如下:
<body>
<div>
<input id="checkall" type="checkbox" value="天" />全部<br/>
<div id="con">
<input type="checkbox" name="checkbox" value="天" />天
<input type="checkbox" name="checkbox" value="空" />空
<input type="checkbox" name="checkbox" value="飞" />飞
<input type="checkbox" name="checkbox" value="来" />来
<input type="checkbox" name="checkbox" value="五" />五
<input type="checkbox" name="checkbox" value="个" />个
<input type="checkbox" name="checkbox" value="字" />字
<input type="checkbox" name="checkbox" value="这" />这
<input type="checkbox" name="checkbox" value="都" />都
<input type="checkbox" name="checkbox" value="不" />不
<input type="checkbox" name="checkbox" value="算" />算
<input type="checkbox" name="checkbox" value="事" />事</div>
</div>
<script src="Scripts/jquery-1.4.1.js" language="javascript"></script>
<script src="JqueryAll.js" language="javascript"></script>
</body>
js中的代码
复制代码 代码如下:
/*js部分
* 绑定批量选定/非选
* 例子
*/
$(function () {
$("#checkall").click(function () {
var stu = $(this).attr("checked");
checkAll(stu);
})
$("input[name='checkbox']").click(function () {
uncheckAll();
})
})
function checkAll(status) {
$("input[name='checkbox']").each(function () {
$(this).attr("checked");
$(this).attr("checked", status);
})
}
function uncheckAll() {
alert($("input[name='checkbox']:checked").length);
if ($("input[name='checkbox']:checked").length == $("input[name='checkbox']").length) {
$("#checkall").attr("checked",true);
}
else {
$("#checkall").attr("checked", false);
}
}
相关文章
jQuery Real Person验证码插件防止表单自动提交
这篇文章为大家介绍了一款jQuery验证码插件Real Person,可以防止自动提交表单2015-11-11jQuery插件passwordStrength密码强度指标详解
这篇文章主要为大家详细介绍了jQuery插件passwordStrength密码强度指标实现代码,感兴趣的小伙伴们可以参考一下2016-06-06
最新评论