jQuery 如何根据值检查复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8316037/
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-27 10:20:32  来源:igfitidea点击:

How can I check checkboxes based on values?

jqueryhtmlcheckbox

提问by Paul

I am returning a JSON data structure that I split and populate my array like so:

我正在返回一个 JSON 数据结构,我将其拆分并填充我的数组,如下所示:

var arrayValues = data.contents.split('|');

// arrayValues = 1,3,4

How can I check the corresponding checkboxes based on the array values?

如何根据数组值检查相应的复选框?

My HTML looks like this:

我的 HTML 如下所示:

 <div id="divID">
     <input type="checkbox" name="test" value="1" /> Test 1<br />
     <input type="checkbox" name="test" value="2" /> Test 2<br />
     <input type="checkbox" name="test" value="3" /> Test 3<br />
     <input type="checkbox" name="test" value="4" /> Test 4<br />
     <input type="checkbox" name="test" value="5" /> Test 5<br />                           
 </div>

回答by ShankarSangoli

Try this using attribute selector

使用属性选择器试试这个

$.each(arrayValues, function(i, val){

   $("input[value='" + val + "']").prop('checked', true);

});

回答by John Hartsock

Javascript

Javascript

$.each(arrayValues, function (index, value) {
  $('input[name="test"][value="' + value.toString() + '"]').prop("checked", true);
});

回答by Christophe Quintard

Simpler :

更简单:

$('input[name="test"]').val(arrayValues);

This will check the values from the array and deselect the others.

这将检查数组中的值并取消选择其他值。

From the JQuery's doc : http://api.jquery.com/val/

来自 JQuery 的文档:http: //api.jquery.com/val/

val() allows you to pass an array of element values. This is useful when working on a jQuery object containing elements like <input type="checkbox">, <input type="radio">, and <option>s inside of a <select>. In this case, the inputs and the options having a value that matches one of the elements of the array will be checked or selected while those having a value that doesn't match one of the elements of the array will be unchecked or unselected, depending on the type.

val() 允许您传递元素值数组。这在处理包含 <input type="checkbox">、<input type="radio"> 和 <option> 等元素的 jQuery 对象时很有用。在这种情况下,将检查或选择具有与数组元素之一匹配的值的输入和选项,而具有与数组元素之一不匹配的值的输入和选项将被取消选中或取消选择,具体取决于在类型上。

回答by JohnP

You can loop through your array and do an attribute search on jQuery

您可以遍历数组并在 jQuery 上进行属性搜索

var i = 0;
while (arrayValues.length < i) {
  var val = arrayValues[i];
  $('#divID input[value="' + val + '"]').prop('checked', 'checked');
  i++;
}

docs : http://api.jquery.com/attribute-equals-selector/

文档:http: //api.jquery.com/attribute-equals-selector/

回答by Smamatti

Okay took me a moment to sort out my error in the code. I guess this question is answered by ShankarSangolinow.

好的,我花了一点时间来整理代码中的错误。我想这个问题现在由ShankarSangoli回答。

I hope you don't mind me posting my solution either way. I'm just curious if this is worse for performance etc.

我希望你不介意我以任何方式发布我的解决方案。我只是好奇这是否对性能等更糟。

var arrayValues = new Array(1,3,4);

$('#divID input').filter(function(){
    return ($.inArray( parseInt($(this).attr('value')), arrayValues)) != -1;
}).attr('checked', 'checked');