将数组值与checkbox id匹配并使checked = false [英] Matching array values with checkbox id and making checked=false

查看:60
本文介绍了将数组值与checkbox id匹配并使checked = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有9个复选框,其ID为1-9。我有一个数组,其中包含var arr = [1,3,4,5,6,7]等数字。现在应该取消选中那些在数组中不存在的复选框(在arr数组编号2,8,9中不存在)。所以对于数字2,8,9应该取消选中chechboxes并且应该检查数组中的数字。

i have 9 checkboxes whose id will be from 1-9. I have an array which consists of numbers like var arr = [1,3,4,5,6,7]. now the checkboxes should be unchecked for those which are not present in the array (in arr array numbers 2,8,9 are not present). so for numbers 2,8,9 the chechboxes should be unchecked and for the numbers in the array should be checked.

chechbox 1: checked




chechbox 3: checked




chechbox 4: checked




chechbox 5: checked




chechbox 6: checked




chechbox 7: checked





我尝试了什么:





What I have tried:

for(var x=0;x<9;x++)
{
if(x==arr[x]
{
document.getElementById(x).checked=true;
}
}

推荐答案

尝试

try
var arr = [1, 3, 4, 5, 6, 7];
        for (var x = 1; x <= 9; x++) { 
            document.getElementById(x).checked = arr.indexOf(x) > -1;
        }



参考 JavaScript数组indexOf()方法 [ ^ ]



演示: - JSFiddle [ ^ ]


这篇关于将数组值与checkbox id匹配并使checked = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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