jQuery 获取具有特定值的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10930048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:58:03  来源:igfitidea点击:

Get checkbox with specific value

jquerycheckbox

提问by palAlaa

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

这是 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 demoof the problem

这是问题的演示

回答by Adil

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.

您可以使用Attribute Equals Selector [name="value"]来获取具有特定值的复选框。也使用prop()而不是attr(),这是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.

描述:选择具有指定属性的元素,其值恰好等于某个值,jQuery doc

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

你也可以使用 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, jQuery doc

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

Edit, asked in comments, "How would look like the answer/find-expression with complex values, like value="Smith, David"

编辑,在评论中询问,“具有复杂值的答案/查找表达式看起来如何,例如 value="Smith, David

You can enclose that kind of values like Smith, Davidon single quotes. You can even use character used by jQuery like #, ., $ etc, see updated fiddle here.

您可以将诸如Smith、David 之类的值括在单引号中。您甚至可以使用 jQuery 使用的字符,如 #、.、$ 等,请在此处查看更新的小提琴。

$("input[type=checkbox][value='#Smith, David$']").attr("checked","true");

回答by thecodeparadox

$(":checkbox").filter(function() {
  return this.value == '5';
}).prop("checked","true");?

DEMO

演示

If you want to use it for different values then make it generic like following:

如果你想将它用于不同的值,那么使它成为通用的,如下所示:

function checkWithValue(val) {
    $(":checkbox").filter(function() {
        return this.value == val;
    }).prop("checked", "true");
}


checkWithValue(5);?

DEMO

演示

回答by bahaddin.yasar

$("input:checkbox[value='5']").attr("checked","true")

回答by Ray Hunter

I had a dynamic values that needed to be looped over. This is what worked for me:

我有一个需要循环的动态值。这对我有用:

for (var i = 0, len = value.length; i < len; i++) {
  $("#form-name").find("input[type=checkbox][name='fName']").filter(function () {
    return this.value == values[i];
  }).prop("checked", true);
}

Hope that helps someone out.

希望能帮助别人。

回答by Afraz Ahmad

Sometimes we want to get checkbox dynamically for a specific value.

有时我们想动态获取特定值的复选框。

$(document).on('click','.at-filter a', function(){
    var aaa = $(this).parent().find("em").html();
    $("input[type=checkbox][value='"+aaa+"']").prop('checked',false)
});

回答by palAlaa

It works also with square practise

它也适用于方形练习

 $(":checkbox").filter(["value":5]).prop("checked","true");

回答by pat capozzi

$("#catalogTBL").find("input[type=checkbox][value='snet']").prop("checked", true);

This was the only way I could get this to work. Looking for a checkbox in a table.

这是我可以让它工作的唯一方法。在表格中查找复选框。

回答by Riz

$(":checkbox[value=5]").attr("checked",true);???