jQuery对checkbox的各种操作
//注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop
//1、根据id获取checkbox
$("#cbCheckbox1");
//2、获取所有的checkbox
$("input[type='checkbox']");//or
$("input[name='cb']");
//3、获取所有选中的checkbox
$("input:checkbox:checked");//or
$("input:[type='checkbox']:checked");//or
$("input[type='checkbox']:checked");//or
$("input:[name='ck']:checked");
//4、获取checkbox值
//用.val()即可,比如:
$("#cbCheckbox1").val();
//5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
//6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr("checked"))
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
});
//7、设置checkbox为选中状态
$('input:checkbox').attr("checked", 'checked');//or
$('input:checkbox').attr("checked", true);
//8、设置checkbox为不选中状态
$('input:checkbox').attr("checked", '');//or
$('input:checkbox').attr("checked", false);
//9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled", "disabled");//or
$("input[type='checkbox']").attr("disabled", true);//or
$("input[type='checkbox']").prop("disabled", true);//or
$("input[type='checkbox']").prop("disabled", "disabled");
//10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").removeAttr("disabled");//or
$("input[type='checkbox']").attr("disabled", false);//or
$("input[type='checkbox']").prop("disabled", "");//or
$("input[type='checkbox']").prop("disabled", false);
代码如下:
<!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>
<title></title>
</head>
<body>
<h3>jQuery操作checkbox
</h3>
<input type="checkbox" id="cbCheckbox1" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" disabled="disabled" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" disabled="true" value="5" />
<br />
<input type="button" id="btnDisabled" value="禁用" onclick="fn_disabled();" />
<input type="button" id="Button1" value="启用" onclick="fn_enable();" /><br />
<input type="button" id="Button2" value="获取选中的值" onclick="getCheckedValues();" /><br />
<input type="button" id="Button3" value="选中第二个" onclick="checkedSecond();" />
<input type="button" id="Button4" value="取消选中第二个" onclick="uncheckedSecond();" /><br />
</body>
</html>
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript"> function fn_disabled() {
//$("input[type='checkbox']").attr("disabled", "disabled");
//$("input[type='checkbox']").attr("disabled", true);
$("input[type='checkbox']").prop("disabled", true);
// $("input[type='checkbox']").prop("disabled", "disabled");
} function fn_enable() {
// $("input[type='checkbox']").removeAttr("disabled");
// $("input[type='checkbox']").attr("disabled", false);
// $("input[type='checkbox']").prop("disabled","");
$("input[type='checkbox']").prop("disabled", false);
} //获取选中的 checkbox的值
function getCheckedValues() {
var arr = [];
$("input[type='checkbox']:checked").each(function (index, item) {//
arr.push($(this).val());
});
alert(arr);
} function checkedSecond() {
// $("input[type='checkbox']:eq(1)").prop("checked", "checked");
$("input[type='checkbox']:eq(1)").prop("checked", true);
} function uncheckedSecond() {
// $("input[type='checkbox']:eq(1)").prop("checked", "");
$("input[type='checkbox']:eq(1)").prop("checked", false);
} $("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {//jquery 1.6以前版本 用 $(this).attr("checked")
alert("选中");
} else {
alert("没有选中");
}
}); </script>
jQuery对checkbox的各种操作的更多相关文章
- jQuery判断checkbox是否选中?操作checkbox(不)选中?
HTML <form action=""> <input type="checkbox" name="c ...
- 1 jquery对checkbox的简单操作
//全选和全不选 votefunction selectAll(){ if($(":checkbox").prop('checked')){ //$(":chec ...
- jQuery获取checkbox选中项等操作及注意事项
jQuery获取checkbox选中项等操作及注意事项 今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下 ...
- jQuery对checkbox选中和取消选中操作
最近做项目发现jQuery对checkbox的全选和非全选操作只有第一次生效,以后就不生效了,不知道是不是jQuery版本库的问题,最终找到了一个解决方案: 把原来的下面这两句: $('input') ...
- JQuery设置checkbox选中或取消等相关操作
$("[name='checkbox']").attr("checked",'true');//全选 $("[name='checkbox']&quo ...
- jquery的checkbox,radio,select等方法总结
jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻.时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对 ...
- 获取checkbox 的选中状态的id、checkbox的一些操作
var id_array=new Array(); $('input[name="id"]:checked').each(function(){ id_array.push($(t ...
- jQuery实现checkbox即点即改,批量计数,以及中间遇到的坑
最近要用jQuery实现一个批量删除操作,效果如下图 最终页面page.html,此页面使用了bootstrap和jQuery,如果没有需要下载一下 <!DOCTYPE html> < ...
- jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax
jQuery jQuery介绍 1.jQuery是一个轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方 ...
随机推荐
- ashx中session的使用
在平常的页面上是用是很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同, 在ashx你无法正常的使用session,即 contex ...
- css之自动换行-设计师零张
自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法 对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素拥 ...
- Python正则匹配字母大小写不敏感在读xml中的应用
需要解决的问题:要匹配字符串,字符串中字母的大小写不确定,如何匹配? 问题出现之前是使用字符串比较的方式,比如要匹配'abc',则用语句: if s == 'abc':#s为需要匹配的字符串 prin ...
- UVA1589 Xiangqi
Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...
- .net 既可以输入又可以选择的dropdownlist
第一.<script src="../../../Common/Scripts/combox.js" type="text/javascript"> ...
- C语言解析日志,存储数据到伯克利DB 2
#编译程序 gcc -o historydb historydb.c -ldb #将2013年8月9日的用户记录写入数据库 (程序自动识别新用户入库,跳过老用户) ./historydb -f .us ...
- C++关于strcpy等函数的安全版本
如下程序: #include <iostream> using namespace std; int main() { ]; strcpy(ch1,"); } 在VS2012上面 ...
- Longest Common Prefix 解答
Question Write a function to find the longest common prefix string amongst an array of strings. Solu ...
- 十分钟搭建自己的hadoop2/CDH4集群
版本及准备 我部署的是hadoop-2.0.0-cdh4.2.0.tar.gz,下载地址为http://archive.cloudera.com/cdh4/cdh/4/hadoop-2.0.0-cdh ...
- spring mvc 安全
1,使用 spring form 标签 防 csrf 攻击 2,标明请求方法:RequestMethod.GET,RequestMethod.POST, PATCH, POST, PUT, and D ...