D3使用classed()添加和删除带复选框的类

Soc*_*ers 3 javascript checkbox d3.js

我无法使用复选框删除我添加的类.选中该复选框以开始.当用户取消选中它时,它会添加一个带有分类('hideRect',true)的"hideRect"类; 这很有效但是当我再次检查该框时,课程不会消失.

这是我的代码:

this.$node.append('input')
    .attr('type', 'checkbox')
    .attr('checked', true)
    .attr('value', 'med')
    .on('click', () => this.updateRectLine());
  }

private updateRectLine() {

  //var rect = this.$node.getElementsByClassName('.MEDICATION');
  var cbMed = this.$node.attr("checked");
  if (cbMed !== true){
      this.$node.selectAll(".MEDICATION").classed('hideRect', true);
  }

  else if (cbMed == true){
      this.$node.selectAll(".MEDICATION").classed('hideRect', false);
  }
Run Code Online (Sandbox Code Playgroud)

}

提前致谢!

Tee*_*eez 5

您需要像这样更新以下功能

private updateRectLine() { 
  var cbMed = this.$node.select("input[type='checkbox']").prop("checked");
  if (!cbMed)
    this.$node.selectAll(".MEDICATION").classed('hideRect', true);
  else
    this.$node.selectAll(".MEDICATION").classed('hideRect', false);
}
Run Code Online (Sandbox Code Playgroud)

.attr()函数只返回复选框初始化的值,要检查复选框的检查状态,您要使用checked复选框元素上的属性