如果要判断Js中的 checked 是否选中,首先取决于你 引用
的 Jquery 的版本。
首先,我引用的是 jQuery v1.11.2 的版本出现的效果如下:
(建议:选用 jquery-1.8.3.min.js 会比较稳定点)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="checkbox" name = "abc" id="aaa" />
</body>
<script src="build/jquery.min.js"></script>
<script>
$(function(){
//都是获取元素是否选中
alert( $("input[name='abc']:checked").val()); //undefined
alert( $("input[name='abc'][checked]").val()); //undefined
alert($("#aaa").attr("checked")); //undefined
alert($(":checkbox:checked").checked) //undefined
alert($("#aaa").prop("checked")); //true
alert($(":checkbox").get(0).checked); //true
});
</script>
</html>
1、获取匹配集合中第一个元素的Property的值
.prop( propertyName )
2、 给匹配元素集合设定一个或多个属性
.prop( propertyName, value )
.prop( map )
.prop( propertyName, function(index, oldPropertyValue) )
.prop()和 .attr()区别
prop是Jquery 从1.6开始提供新的方法。
To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.
根据官方的建议:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()
那么,什么时候使用attr(),什么时候使用prop()?
1.添加属性名称该属性就会生效应该使用prop();
2.是有true,false两个属性使用prop();
3.其他则使用attr();
项目中jquery升级的时候要注意这点!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有