遍历数组以启用复选框 [英] Loop through an Array to enable checkboxes

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

问题描述

我有一组复选框和一个数组,其中包含应选择其复选框的索引.我正在尝试遍历数组及其中的每个索引.我制作了一个示例jsFiddle,让大家了解我正在尝试做的事情.如果这使事情变得更容易,我也有JQuery库. http://jsfiddle.net/7EetA/1/

I'm have a set of checkboxes and an array that contains the index of which checkboxes should be selected. I'm trying to loop through the array and for each index in it. I made a sample jsFiddle to give you guys an idea of what I'm trying to do. I have the JQuery library also if that makes things easier. http://jsfiddle.net/7EetA/1/

推荐答案

尝试一下:

var arrx=new Array();
arrx[0]=4;       
arrx[1]=5;
arrx[2]=3;
arrx[3]=1;
for (var i = 0; i < arrx.length; i++) {
    document.getElementsByName('cal')[arrx[i]].checked = true;
}
​

不需要jQuery! jsFiddle示例

No jQuery needed! jsFiddle example

这篇关于遍历数组以启用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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