我想获得具有特定价值的复选框,并使其检查..
我这样做
$(":checkbox").filter({"value":5}).attr("checked","true");
这里是html
<input type="checkbox" name="priv" value="1"/> <input type="checkbox" name="priv" value="2"/> <input type="checkbox" name="priv" value="3"/> <input type="checkbox" name="priv" value="4"/> <input type="checkbox" name="priv" value="5"/> <input type="checkbox" name="priv" value="6"/> <input type="checkbox" name="priv" value="7"/> <input type="checkbox" name="priv" value="8"/>
这是一个demo的问题
解决方法
您可以使用
Attribute Equals Selector [name=”value”]获得具有特定价值的复选框。也可以使用
prop()而不是
attr(),因为这是
jQuery doc推荐的方式。
$(":checkbox[value=4]").prop("checked","true");
要么
$("input[type=checkbox][value=5]").prop("checked",true);
说明:选择具有指定属性的元素,其值等于特定值jQuery doc。
您也可以使用attr(),但是prop更适合布尔属性。
$(":checkbox[value=4]").attr("checked","true");
As of jQuery 1.6,the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked,selected,or disabled state of form elements,use the .prop() method,07003
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。