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.实现动画效果.方 ...
随机推荐
- 搭建phonegap开发环境,搭建安卓开发环境
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- VC2010的破解方法(针对旗舰版)
VS2010 正式版破解方法详解 全球开发者最为瞩目的Visual Studio 2010开发工具在4月12日正式发布,现为大家制作一个简单的破解教程有两种方法,操作不一样,原都一样(针对旗舰版,其他 ...
- C语言生产随机数的方法
尽管在计算机中并没有一个真正的随机数发生器,但是可以做到使产生的数字的重复率很低,以至于它们看起来是随机的.实现这一功能的程序叫做伪随机数发生器. 有关如何产生随机数的理论有许多,这里不讨论这些理论及 ...
- #include<iostream>与#include<iostream.h>的区别
转载于祝长洋的BLOG:http://blog.sina.com.cn/s/blog_514b5f600100ayks.h ...
- hdr(host), hdr_beg(host) , path_beg
ACL derivatives : hdr([<name>[,<occ>]]) : exact string match 字符串精确匹配 hdr_beg([<name&g ...
- hihoCoder 1092 : Have Lunch Together
题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...
- poj1552
Doubles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18824 Accepted: 10846 Descrip ...
- ios 导航栏和旋屏
1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...
- linux下安装swftools工具
swfTools是一种实用工具与Adobe Flash文件(swf文件)工作的集合.可以把(pdf/gif/png/jpeg/jpg/font/wav) 7种格式转换为swf文件.一般常用于文件在线浏 ...