保存复选框数组的第一个选定选项

时间:2015-04-09 10:22:14

标签: javascript loops

假设我有一个checkbox数组,允许进行多项选择。我想只保存变量中的第一个选择但仍然记录并保存其他选择(在另一个变量中)。类似的东西:

First_Selected = this_selection (x)
Other_Selected = these_selections(1,2,3)

使用下面的循环我将如何实现这个?

   var Selection_one = " ";
    var other_Selections = " ";
    for (i = 0; i < tbls.length; i++) {
        Selection_one = ?? ;
        other_Selections += ?? ;
    }

2 个答案:

答案 0 :(得分:0)

使用.slice()选择第一个之后的所有元素,然后使用.join()连接所有值。

var selection_one = tbls[0];
var other_selections = tbls.slice(1).join(' ');

答案 1 :(得分:0)

用你的逻辑

var Selection_one = " ";
    var other_Selections = " ";
    for (i = 0; i < tbls.length; i++) {
       if(i == 0)
           Selection_one = tbls[i];
        other_Selections += tbls[i] + ',' ;
    }