te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - D3 using classed() to add and remove class with checkbox - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3 using classed() to add and remove class with checkbox - Stack Overflow

programmeradmin2浏览0评论

I am having trouble removing a class that I have added using a checkbox. The checkbox is checked to begin with. When it is unchecked by the user it adds a "hideRect" class with classed('hideRect', true); this works great BUT when I check the box again the class doesn't go away.

Here is my code:

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);
  }

}

thanks in advance!

I am having trouble removing a class that I have added using a checkbox. The checkbox is checked to begin with. When it is unchecked by the user it adds a "hideRect" class with classed('hideRect', true); this works great BUT when I check the box again the class doesn't go away.

Here is my code:

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);
  }

}

thanks in advance!

Share Improve this question asked Jul 3, 2017 at 19:27 Sockness_RogersSockness_Rogers 1,6833 gold badges13 silver badges23 bronze badges 3
  • What's the relationship between the elements w/ class .MEDICATION and the checkbox? On the first line of your function, try logging the value of that selection to see if it includes the elements you expect it to. – anbnyc Commented Jul 3, 2017 at 19:35
  • Hey @anbnyc thanks for the reply! I can see the class added to the right element in the console so I know it targets the right selection but it wont remove the class! – Sockness_Rogers Commented Jul 3, 2017 at 19:42
  • Can't think of what else might be the problem. If you can provide a reproducible example (like a JSFiddle) might be able to tell more. This probably isn't related, but you may want triple equals (cbMed === true) to check after the else if. – anbnyc Commented Jul 3, 2017 at 20:09
Add a ment  | 

1 Answer 1

Reset to default 13

You need to update the below function like this

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);
}

.attr() function only returns the value the checkbox was initialized to, to check for a checkbox's check state, you want to use property checked present on check box elements

发布评论

评论列表(0)

  1. 暂无评论