<!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">
<meta charset='utf-8'/>
<head>
<title>全选,不全选,反选</title>
<script src="jquery-2.1.4.min.js" type="text/javascript"></script>
</head>
<body>
<ul id="list">
<li><label><input type="checkbox" value="1"> 1.时间都去哪儿了</label></li>
<li><label><input type="checkbox" value="2"> 2.海阔天空</label></li>
<li><label><input type="checkbox" value="3"> 3.真的爱你</label></li>
<li><label><input type="checkbox" value="4"> 4.不再犹豫</label></li>
<li><label><input type="checkbox" value="5"> 5.光辉岁月</label></li>
<li><label><input type="checkbox" value="6"> 6.喜欢妳</label></li>
</ul>
<input type="checkbox" id="all">
<input type="button" value="全选" class="btn" id="selectAll">
<input type="button" value="全不选" class="btn" id="unSelect">
<input type="button" value="反选" class="btn" id="reverse">
<input type="button" value="获得选中的所有值" class="btn" id="getValue">
</body>
<script type="text/javascript">
$(document).ready(function(){
//全选或全不选
$("#all").click(function(){
if(this.checked){
$("input[type=checkbox]").prop("checked",true);
}else{
$("input[type=checkbox]").prop("checked",false);
}
});
//全选
$("#selectAll").click(function () {
$("#list :checkbox,#all").prop("checked", true);
});
//全不选
$("#unSelect").click(function () {
$("#list :checkbox,#all").prop("checked", false);
});
//反选
$("#reverse").click(function () {
$("#list :checkbox").each(function () {
$(this).prop("checked", !$(this).prop("checked"));
});
allchk();
}); //设置全选复选框
$("#list :checkbox").click(function(){
allchk();
}); //获取选中选项的值
$("#getValue").click(function(){
str = '';
n = '';
$("#list :checkbox").each(function(){
if(this.checked){
str = str + n + $(this).val();
n = ',';
}
});
alert(str);
}); }) function allchk(){
var chknum = $("#list :checkbox").size();//选项总个数
var chk = 0;
$("#list :checkbox").each(function () {
if($(this).attr("checked")==true){
chk++;
}
});
if(chknum==chk){//全选
$("#all").attr("checked",true);
}else{//不全选
$("#all").attr("checked",false);
}
} </script>
</html>

jquery全选,全不选,反选,获取选择框的值的更多相关文章

  1. js/jquery获取文本框的值与改变文本框的值

    我们就用它来学习获取文本框的值及改变文本框的值. 代码如下 复制代码 <script>function get1(){ document.getElementById("txtb ...

  2. jquery attr()方法 添加,修改,获取对象的属性值。

    jquery attr()方法 添加,修改,获取对象的属性值. jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到 ...

  3. jquery设置文本框值 与获取文本框的值

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. jquery 如何获取单选框的值

    jquery 如何获取单选框的值   获取单选框的值有三种方式: 1.$('input:radio:checked').val():2.$("input[type='radio']:chec ...

  5. React-Native获取文本框的值

    要想获取文本框的值,首先我们需要看一下官方文档的解释: 这里的意思是说当文本框的内容改变的时候,文本框的输入的内容就会作为一个参数进行传递.因此我们就可以获取到文本框里面的内容就好了. constru ...

  6. js获取单选框的值

    js获取单选框的值 var lx= $("input[name='lx']:checked").val();

  7. HTML--JS 获取选择框信息

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. php如何获取单选复选和选择框的值

    1.很久没有写基础的东西了复习一下(往往简单的东西才复杂) <body> 选择语句 <form action="demo.php" method="po ...

  9. select2 智能补全模糊查询select2的下拉选择框使用

    我们在上篇文章中已经在SpringMVC基础框架的基础上应用了BootStrap的后台框架,在此基础上记录select2的使用. 应用bootstrap模板 基础项目源码下载地址为: SpringMV ...

  10. jQuery name属性与checked属性结合获取表单元素值

    var paytype = $("input[name='paytype']:checked").val(); alert(paytype); input元素下名称为paytype ...

随机推荐

  1. vue 组件之间事件触发($emit)与event Bus($on)的用法说明

    组件之间事件触发 新增按钮组件: 操作按钮组合组件: 此时有个需求就是,无论是哪个按钮,如果改变了列表中的数据,列表需要实时更新数据. 此时就需要用到组件间的事件触发. 父子组件之间事件触发可以使用$ ...

  2. 第12组 Beta冲刺 (2/5)

    1.1基本情况 ·队名:美少女战士 ·组长博客:https://www.cnblogs.com/yaningscnblogs/p/14016602.html ·作业博客:https://edu.cnb ...

  3. 线程安全与数据结构JAVA

    线程 线程与进程本质的区别在于每个进程拥有自己的一整套变量, 而线程之间可以有共享变量.另外创建.销毁一个线程的代价比启动新进程的代价要小. 在java中,没有可以强制线程终止的方法,然而, inte ...

  4. 划分数据集时出现PermissionError: [Errno 13] Permission denied:

    PermissionError: [Errno 13] Permission denied: [errno 13]权限被拒绝 错误的原因可能是文件找不到,或者被占用,或者无权限访问,或者打开的不是文件 ...

  5. 使用 IntersectionObserver API 遇到的一些问题

    root 设指定为 document.body 时不会触发更新 See the Pen document.body and IntersectionObserver by y1j2x34 (@y1j2 ...

  6. 编译GO报错:cgo: C compiler

    cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH ...

  7. [Unity热更新]Addressables

    参考链接: https://linxinfa.blog.csdn.net/article/details/122390621?spm=1001.2014.3001.5502 总结: 1.

  8. 使用ipmitool配置ipmi(远程控制卡)

    使用ipmitool配置ipmi(远程控制卡) 在centos安装OpenIPMI: yum install OpenIPMI OpenIPMI-tools 设置开机启动 chkconfig ipmi ...

  9. 【Keil】浅学一下keil中的.sct文件

    [Keil]浅学一下keil中的.sct文件 最近重新捣鼓了acfly的源码,有了新的有趣发现,当然,过程并不有趣. 起因 clone下来我去年提交的代码,编译 ...... 报错辣! linking ...

  10. LightOJ1298 One Theorem, One Year (欧拉函数dp)

    题意:给你almost-K-First-P-Prime, 如果一个数x有k个质因子,且这k个质因子包含且仅包含前p个质数满足条件. 让你求Σφ(x): 思路:首先我们这p个因子一定要有,也就是剩下k- ...