jquery 选择 取消 checkbox
2015-09-09 23:34
633 查看
jquery选择checkbox示例代码,包括四个功能,分别是查看选择的checkbox、全选、取消全选、反选。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery/jquery-1.6.1.min.js" ></script>
<title>JQUERY使用CHECKBOX</title>
</head>
<body>
<form id="editForm" name="editForm" action="" method="post">
<input type="checkbox" name="ckname" value="1"/>1<br />
<input type="checkbox" name="ckname" value="2"/>2<br />
<input type="checkbox" name="ckname" value="3"/>3<br />
<input type="button" value="查看选择的checkbox" onclick="showCheckBox();"/>
<input type="button" value="全选" onclick="checkAllBox();"/>
<input type="button" value="取消全选" onclick="cancelcheckAllBox();"/>
<input type="button" value="反选" onclick="reverseSelectAllBox();"/>
</form>
</body>
</html>
<script type="text/javascript">
/**
* 获取选中的checkbox,并显示选中的值
*/
function showCheckBox() {
$('input[name="ckname"]:checked').each(function(index, element) {
alert("index:" + index + " element:" + element.value);
});
}
/**
* 全选checkbox
*/
function checkAllBox() {
$('input[name="ckname"]').attr("checked", true);
}
/*
* 取消全选
*/
function cancelcheckAllBox() {
$('input[name="ckname"]').attr("checked", false);
}
/*
* 反选
*/
function reverseSelectAllBox() {
$('input[name="ckname"]').each(function(index, element) {
if (element.checked) {
element.checked = false;
//$(this).removeAttr("checked");
} else {
//element.checked = true;
$(this).attr("checked", true);
}
});
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery/jquery-1.6.1.min.js" ></script>
<title>JQUERY使用CHECKBOX</title>
</head>
<body>
<form id="editForm" name="editForm" action="" method="post">
<input type="checkbox" name="ckname" value="1"/>1<br />
<input type="checkbox" name="ckname" value="2"/>2<br />
<input type="checkbox" name="ckname" value="3"/>3<br />
<input type="button" value="查看选择的checkbox" onclick="showCheckBox();"/>
<input type="button" value="全选" onclick="checkAllBox();"/>
<input type="button" value="取消全选" onclick="cancelcheckAllBox();"/>
<input type="button" value="反选" onclick="reverseSelectAllBox();"/>
</form>
</body>
</html>
<script type="text/javascript">
/**
* 获取选中的checkbox,并显示选中的值
*/
function showCheckBox() {
$('input[name="ckname"]:checked').each(function(index, element) {
alert("index:" + index + " element:" + element.value);
});
}
/**
* 全选checkbox
*/
function checkAllBox() {
$('input[name="ckname"]').attr("checked", true);
}
/*
* 取消全选
*/
function cancelcheckAllBox() {
$('input[name="ckname"]').attr("checked", false);
}
/*
* 反选
*/
function reverseSelectAllBox() {
$('input[name="ckname"]').each(function(index, element) {
if (element.checked) {
element.checked = false;
//$(this).removeAttr("checked");
} else {
//element.checked = true;
$(this).attr("checked", true);
}
});
}
</script>
相关文章推荐
- jquery封装 [ 限制文本框只能输入数字和小数] 语句,简单调用即可
- jquery.validate 验证
- jQuery中 wrap() wrapAll() 与 wrapInner()的区别
- 02.Jquery Mobile介绍以及Jquery Mobile页面与对话框
- Web前端——JQuery的进阶(测试使用 jQuery进行指定的操作)
- jquery mobile获取表单input数据值
- jQuery EasyUI Datagrid性能优化专题(转)
- java 版 jquery jcrop截图并上传
- jQuery EasyUI Datagrid VirtualScrollView视图简单分析
- jquery 使用 xhrFields 来获取下载进度的方法
- jQuery 的插件 dataTables
- jquery 获取鼠标坐标
- jquery插件之拖拽
- Jquery+Json+ajax实现异步查询显示数据
- 纯js与jquery两种方法,获得某元素同级节点内容,进行值替换
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
- jquery的ajax和getJson跨域获取json数据
- jQuery的css()方法
- jquery获取radio选中的值
- jquery获取select选中的值