代码如下所示:
<div id="list">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
</div>
在另一个 html Pane (一个单独的模板)中,我想将所有这些复选框(checked
/unchecked
) bool 值存储到一个数组中。我所做的看起来像:
var array = [];
var checkboxli = document.getElementsByTagName("input");
for (var i = 0; i < checkboxli.length; i++)
{
array.push($("#input.prop('checked')"));
}
但是,这是行不通的。我还有其他使用标签名称“input
”的模板,因此我需要将标签名称限制为 “#list”
id 下的标签名称(也许是某种 css 选择器) .目前,document.getElementsByTagName("input")
和 $("#input.prop('checked')")
都不起作用。可能还有其他语法问题。请帮我解决。谢谢。
编辑:看来我没有很好地传达我的意图。这是我想从列表中删除的内容: 一个看起来像的数组
[true, false, true, true, true...]
其中每个 bool 值代表对应的输入复选框是否被选中。
最佳答案
因为你已经在使用 jquery,你可以这样做:
假设这个 HTML
<div id="list">
<input type="checkbox" id="1" checked="checked">
<input type="checkbox" id="2">
<input type="checkbox" id="3" checked="checked">
</div>
还有这个脚本:
var array = [];
$("input[type='checkbox']","#list").each(function(){
array.push($(this).is(":checked"));
});
你会得到这样的东西:
array = [ true, false, true ];
关于javascript - 如何使用 localstorage 将列表中的所有复选框 bool 值存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32596251/