如何根据值检查复选框? [英] How can I check checkboxes based on values?
本文介绍了如何根据值检查复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我返回一个JSON数据结构,我拆分并填充我的数组,如下:
I am returning a JSON data structure that I split and populate my array like so:
var arrayValues = data.contents.split('|');
// arrayValues = 1,3,4
相应的复选框基于数组值?
How can I check the corresponding checkboxes based on the array values?
我的HTML看起来像这样:
My HTML looks like this:
<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>
推荐答案
使用属性选择器尝试此操作
Try this using attribute selector
$.each(arrayValues, function(i, val){
$("input[value='" + val + "']").prop('checked', true);
});
这篇关于如何根据值检查复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文