jQuery判断复选框checkbox的选中状态
通过jQuery设置复选框为选中状态
复选框
<input type="checkbox"/>
错误代码:
$("input").attr("checked","checked");
设置以后checkbox变成选中状态,用Chrome调试看了一下,checkbox中确实有checked属性,而且值为checked,根据W3C的表单规范,checked属性是一个布尔属性,这意味着只要该 attribute 存在,即使它没有值,或是一个空字符串,该属性对应的 property 默认就是 true,也就是说和attribute的值没有关系。但是一旦通过 prop 将 property 设置为false,则使用 attr 将无法使该 checkbox 改变为选中状态。
正确代码:
$("input").prop("checked",true);
设置后复选框为选中状态了。
原因:
attributes(属性) 和 properties(特性) 之间的差异在特定情况下是很重要的。
jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。
从 jQuery 1.6 开始, .prop()方法返回 property 的值,而 .attr() 方法返回 attributes 的值。
以下推荐的是兼容浏览器的三种写法,判断 checkbox 元素的 checked 属性是否为"真" (是否被选中),elem是 JavaScript 对象:
if ( $(elem).prop("checked") ){}
if ( $(elem).is(":checked") ){}
if ( elem.checked ) {}
代码示例:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
</head>
<body>
<div>
<input type="button" value="点击1" onclick="add1();" />
<input type="button" value="点击2" onclick="add2();" />
<input type="button" value="点击3" onclick="add3();" />
<input type="checkbox" id="myinput" />
</div>
</body>
<script type="text/javascript">
//添加选中和未选中状态
function add1(){
if($("#myinput").prop("checked")){
$("#myinput").prop("checked",false);
}else{
$("#myinput").prop("checked",true);
} }
function add2(){
if($("#myinput").is(":checked")){
$("#myinput").prop("checked",false);
}else{
$("#myinput").prop("checked",true);
}
}
function add3(){
if(document.getElementById("myinput").checked){
$("#myinput").prop("checked",false);
}else{
$("#myinput").prop("checked",true);
}
}
</script>
</html>
jQuery判断复选框checkbox的选中状态的更多相关文章
- [jQuery] 判断复选框checkbox是否选中checked
返回值是true/false method 1: $("#register").click(function(){ if($("#accept").get(0) ...
- jquery判断复选框checkbox是否被选中
jquery判断复选框checkbox是否被选中 使用is方法 //如果选中返回true //如果未选中返回false .is(':checked');
- jQuery判断复选框是否被选中的3种方式
页面部分: <input type="checkbox" id="cbx" /><label for="cbx"& ...
- jquery判断复选框是否被选中
$("#isUse").click(function(){ if($(this).is(':checked')){ $(this).attr('checked','checked' ...
- 根据复选框checkbox的选中状态来打开或关闭隐藏层
HTML: <input type="checkbox" id="check-expert"> <div id="expert&q ...
- jquery判断复选框是否选中
jquery判断复选框是否被选中 $(function(){ $(document).on("click", ".checkbox",function(){ v ...
- jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等
转载:https://blog.csdn.net/chenchunlin526/article/details/77448168 jQuery操作复选框checkbox技巧总结 --- 设置选中.取消 ...
- php 判断复选框checkbox是否被选中
php 判断复选框checkbox是否被选中 复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...
- jquery操作复选框(checkbox)十二技巧
jquery操作复选框(checkbox)的12个小技巧. 1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$( ...
随机推荐
- spring的ioc与aop原理
ioc(反向控制) 原理: 在编码阶段,既没有实例化对象,也没有设置依赖关系,而把它交给Spring,由Spring在运行阶段实例化.组装对象.这种做法颠覆了传统的写代码实例化.组装对象.然后一 ...
- 记号一下selenium+Firefox自动下载的参数
参考: https://blog.csdn.net/wxstar8/article/details/80782556 https://blog.csdn.net/xiaoguanyusb/articl ...
- Android Studio--按钮跳转新页
MainActivity.xml: <Button android:id="@+id/btnGo" android:layout_width="wrap_conte ...
- install mysql from source and troubleshooting example
I tried to install MySQL 5.7 from source file and upgrading previous MySQL version to the lastest 5. ...
- OpenStack 安装:glance 安装
接上一篇keystone, 这一篇介绍glance服务: 在开始操作之前,先用source环境变量,然后创建glance 用户,并设置密码为glance [root@linux-node1 ~]#op ...
- 目前php连接mysql的主要方式
mysqli和PDO, 其中mysqli可以有面向过程,面向对象两种方式.而pdo只有面向对象的方式. <?php // $mysql_server = "localhost" ...
- 528. Random Pick with Weight index的随机发生器
[抄题]: Given an array w of positive integers, where w[i] describes the weight of index i, write a fun ...
- Linux安装配置Redis,CentOS7下安装Redis教程
1.下载Redis wget https://download.redis.io/releases/redis-3.0.4.tar.gz 2 . 解压Redis .tar.gz 3 . 编译安装Red ...
- Mysql 提升大数据表的拷贝效率
工作上会经常遇到量级比较大的数据表 :场景: 该数据表需要进行alter操作 比如增加一个字段,减少一个字段. 这个在一个几万级别数据量的数据表可以直接进行alter表操作,但是要在一个接近1000 ...
- sql存储过程进行条件筛选
1.创建临时表,把存储过程结果集保存到临时表,对临时表进行筛选. Create Table #TmpTable(FieldList) Insert Into #TmpTable Exec StoreP ...