为什么不能将字符串添加到javascript数组中?

时间:2015-10-05 10:32:10

标签: javascript

我尝试使用JavaScript语言将复选框ID添加到数组中,这是我的代码:

function checkBoxClick(e) {
    alert("a");
    var arr = new Array();
    var rule = new Array();
    var chkId = $(e).attr("id");
    var tempId = "";
    var tempElementId = "";
    $("input[class='singlechk']").each(function () {
        var elementId = $(this).attr("id");
        var larr = $(this).attr("id").split('-');
        tempElementId = elementId;
        if (tempId == "") {
            tempId = larr[0];
        } else {
            if (tempId != larr[0]) {
                rule.push(arr);
                arr = [];
                arr.push(tempElementId);
            } else {
                arr.push(tempElementId);
            }
        }
    });
}

我是每个复选框,哪个类是“singlechk”。将id推入数组arr,但每次它都会添加最后一个元素,并且第一个元素无法推入数组。

这是我的HTML代码:

<div class="container">
    <h3 id="question_1">饭菜质量
        <span>(单选)</span>
    </h3>
    <br />
    <input name="wjdc" class="singlechk" id="item_1-1" type="checkbox" onclick="checkBoxClick(this)" />
    <span>一般</span>
    <br />
    <input name="wjdc" class="singlechk" id="item_1-2" type="checkbox" onclick="checkBoxClick(this)" />
    <span>很好</span>
    <h3 id="question_2">就餐环境
        <span>(单选)</span>
    </h3>
    <br />
    <input name="wjdc" class="singlechk" id="item_2-3" type="checkbox" onclick="checkBoxClick(this)" />
    <span>很好</span>
    <br />
    <input name="wjdc" class="singlechk" id="item_2-4" type="checkbox" onclick="checkBoxClick(this)" />
    <span>一般</span>
</div>

这是正确的结果:

var rule = [["item_1-1", "item_1-2"], ["item_2-3", "item_2-4"]];

我的问题是:为什么阵列远离输出

var rule = [["item_1-2"], ["item_2-4"]];

2 个答案:

答案 0 :(得分:2)

阵列在else条件下重置,然后新项目push加入其中。因此,在循环结束后,数组将只包含最后一个元素。

删除

arr = [];

另一件事是

rule.push(arr);

将整个数组作为rule数组中的元素推送。

要在另一个数组中添加数组元素,请使用concat

rule.concat(arr);

答案 1 :(得分:1)

您可以尝试类似

的内容

function checkBoxClick(e) {
  var rule = new Array(),
    tmp = {};
  $("input.singlechk:checked").each(function() {
    var elementId = this.id;
    var larr = elementId.split('-');
    if (!tmp[larr[0]]) {
      tmp[larr[0]] = [];
      rule.push(tmp[larr[0]]);
    }
    tmp[larr[0]].push(elementId);
  });
  snippet.log(JSON.stringify(rule))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<div class="container">
  <h3 id="question_1">饭菜质量<span>(单选)</span></h3>
  <br />
  <input name="wjdc" class="singlechk" id="item_1-1" type="checkbox" onclick="checkBoxClick(this)" />
  <span>一般</span>
  <br />
  <input name="wjdc" class="singlechk" id="item_1-2" type="checkbox" onclick="checkBoxClick(this)" />
  <span>很好</span>
  <h3 id="question_2">就餐环境<span>(单选)</span></h3>
  <br />
  <input name="wjdc" class="singlechk" id="item_2-3" type="checkbox" onclick="checkBoxClick(this)" />
  <span>很好</span>
  <br />
  <input name="wjdc" class="singlechk" id="item_2-4" type="checkbox" onclick="checkBoxClick(this)" />
  <span>一般</span>
</div>

相关问题