2015-10-05 68 views
-2

我尝试复选框ID添加到使用的JavaScript语言的数组,这是我的代码:为什么不能将字符串添加到JavaScript数组中?

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"]]; 
+0

? – Satpal

+0

我想清空旧数组 – Dolphin

回答

2

该阵列在else条件中重置,然后编辑一个新项目push。所以,在循环结束后,数组将只包含最后一个元素。

删除

arr = []; 

的另一件事是

rule.push(arr); 

将整个阵列推作为rule阵列内的元件。

要将数组元素添加到另一个数组中,请使用concat。 `;

rule.concat(arr); 
+0

重置之前,我已经将数组移动到另一个数组规则 – Dolphin

+0

@Dolphin检查更新的答案 – Tushar

1

可你为什么要使用`ARR = []尝试像

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>

相关问题