获取具有特定值的复选框 [英] Get checkbox with specific value

查看:112
本文介绍了获取具有特定值的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获得具有specfic值的复选框并进行检查。



我这样做

  $(:checkbox)。filter({value:5})。attr(checked,true); 



这里是html

  < input type =checkboxname =privvalue =1 /> 
< input type =checkboxname =privvalue =2/>
< input type =checkboxname =privvalue =3/>
< input type =checkboxname =privvalue =4/>
< input type =checkboxname =privvalue =5/>
< input type =checkboxname =privvalue =6/>
< input type =checkboxname =privvalue =7/>
< input type =checkboxname =privvalue =8/>

这是一个演示的问题

解决方案

您可以使用属性等于选择器[name =value] 获取具有特定值的复选框。还可以使用 prop()而不是 attr (),因为这是 jQuery doc 推荐的方式。



现场演示

  $(:checkbox [value = 4])。prop(checked,true); 

  $(input [type = checkbox] [value = 5])。prop(checked,true); 

说明:选择具有指定属性且值完全相等的元素到某个值, jQuery文档



您也可以使用attr(),但是prop更适合布尔属性。

  $(:checkbox [value = 4])。attr(checked,true); 




从jQuery 1.6开始,.attr()方法返回undefined属性
尚未设置。要检索和更改DOM属性(例如
检查,选择或禁用表单元素的状态),请使用
.prop()方法, jQuery文档



I want to get checkbox with specfic value and make it checked..

I make like this

$(":checkbox").filter({"value":5}).attr("checked","true");​

and here is the 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"/>​

here's a demo of the problem

解决方案

You can use Attribute Equals Selector [name="value"] to get the checkboxes with particular value. Also use prop() instead of attr() as that is recommended way by jQuery doc.

Live Demo

$(":checkbox[value=4]").prop("checked","true");

or

$("input[type=checkbox][value=5]").prop("checked",true);

Description: Selects elements that have the specified attribute with a value exactly equal to a certain value, jQuery doc.

You can also do it Using attr() but prop is more appropriate for boolean properties.

$(":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, jQuery doc

这篇关于获取具有特定值的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆