我对这一切都很陌生,我真的需要帮助。我已经研究这个问题好几个小时了。
我有这些复选框。我使用 cfoutput 来生成它们,并为它们提供查询中的 SID 值。
<cfoutput query="getvalues">
<div><input type="checkbox" name="chk" id=#getvalues.SID# value=#getvalues.SID# class="chkbxs">
</cfoutput>
<input type="button" name="PrintSelected" value="Print Selected" onclick="printTextArea()">
我唯一想做的就是获取这些复选框的值并将它们存储在数组中。 getElementsByClassName 返回一个 html 集合。有人告诉我需要循环 html 集合,然后将值存储在一个新数组中,这就是我在下面尝试的方法,但这不起作用。
<script type="text/javascript">
function printTextArea() {
var myList = document.getElementsByClassName("chkbxs");
var newList = [];
for (var i = 0; i < myList.length; i++) {
newList.push(myList[i].value);
}
for (var j = 0; j < newList.length j++)
{
alert (newList[j]);
}
}
</script>
如有任何帮助,我们将不胜感激。
最佳答案
为什么要将数组项设置为 console.log(myList[i].value);
?
console.log()
仅返回 undefined
。
只需将行更改为以下内容:
newList[i] = myList[i].value;
关于javascript - 将 HTML Collection 中的值存储在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841097/