[英]Check whether the checkbox are checked or not
I have checkbox in my d3 tree.我的 d3 树中有复选框。 my code is as follows.
我的代码如下。
nodeEnter.append('foreignObject').attr('width', '20')
.attr("x", 200 / 2)
.attr("y", 1)
.attr('height', '20').append('xhtml:input')
.attr('type', 'checkbox')
// An on click function for the checkboxes
.on("click",function(d){
console.log(d)
});
From this code I am able to catch data on click of checkbox.从这段代码中,我可以在单击复选框时捕获数据。 But with this I want to know whether the check box is checked or not.
但是有了这个我想知道复选框是否被选中。 How can I do this?
我怎样才能做到这一点?
In the click function you can access the node being clicked with this
, and see if it is checked with this.checked
在点击 function 可以访问被点击的节点
this
,看看它是否被this.checked
.on("click",function(d){
var check = this.checked;
console.log(check,d);
});
Alternatively, you can use d3 to achieve the same thing with d3.select(this).property("checked");
或者,您可以使用 d3 来实现与
d3.select(this).property("checked");
As you are using d3v5 or earlier, you can also access the clicked node as follows:当您使用 d3v5 或更早版本时,您还可以访问单击的节点,如下所示:
.on("click",function(d,i,nodes){
var check = nodes[i].checked;
console.log(check,d);
});
Here's the first approach:这是第一种方法:
var svg = d3.select("body").append("svg").attr("width", 400).attr("height", 300); var data = [1,2]; var nodeEnter = svg.selectAll(null).data(data).enter(); nodeEnter.append('foreignObject').attr('width', '20').attr("x", (d,i) => i*50+50).attr("y", 1).attr('height', '20').append('xhtml:input').attr('type', 'checkbox') // An on click function for the checkboxes.on("click",function(d){ var check = this.checked; console.log(check,d); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.