好的,我一直在尝试根据数据集中的列数动态地将一些复选框附加到 div 中。所以我认为 d3 将是要走的路,只需将输入附加适当的属性和一些文本作为从数据确定的标签。我试过下面的代码;
d3.select("body").selectAll("input")
.data([11, 22, 33, 44])
.enter().append("input")
.attr("checked", true)
.attr("type", "checkbox")
.attr("id", function(d,i) { return i; })
.attr("onClick", "change(this)")
.attr("for", function(d,i) { return i; })
.text(function(d) { return d; });
这导致页面上有 4 个复选框,但没有标签。
真正奇怪的是,当我检查元素时,生成的 html 似乎就是我所追求的,如下所示。
<input checked="true" type="checkbox" id="0" onclick="change(this)" for="0">11</input>
<input checked="true" type="checkbox" id="1" onclick="change(this)" for="1">22</input>
<input checked="true" type="checkbox" id="2" onclick="change(this)" for="2">33</input>
<input checked="true" type="checkbox" id="3" onclick="change(this)" for="3">44</input>
当我在页面上使用它时,我得到的正是我所追求的。
我确定我错过了一些非常简单的东西,但是对于我的生活,我看不到它是什么。任何帮助感激地接受!