复选框检查/取消选中使用d3 [英] checkbox check/uncheck using d3
问题描述
我无法使用d3选择检查和取消选中复选框。下面的代码似乎不工作。我想在按下检查时选中所有复选框,并在按取消选中时取消选中所有复选框。
I am having difficulty checking and unchecking checkboxes using d3 selection. The following code doesn't seem to work. I want all check boxes to be checked when 'check' is pressed, and all checkboxes unchecked when 'uncheck' is pressed.
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<button type='button' onclick='checkAll()'>Check</button>
<button type='button' onclick='uncheckAll()'>Uncheck</button>
<script src="http://d3js.org/d3.v2.min.js?2.10.0"></script>
<script>
function checkAll(){
d3.selectAll('input').attr('checked','true');
}
function uncheckAll(){
d3.selectAll('input').attr('checked','false');
}
</script>
</body>
推荐答案
/ code>而不是 attr()
:
Use property()
instead of attr()
:
function checkAll() {
d3.selectAll('input').property('checked', true);
}
function uncheckAll() {
d3.selectAll('input').property('checked', false);
}
从 https://github.com/mbostock/d3/wiki/Selections#wiki-property :
一些HTML元素具有使用标准属性或样式无法寻址的特殊属性。例如,表单文本字段具有
值
字符串属性,复选框具有检查
布尔属性<强>。您可以使用属性
运算符来获取或设置这些属性。
Some HTML elements have special properties that are not addressable using standard attributes or styles. For example, form text fields have a
value
string property, and checkboxes have achecked
boolean property. You can use theproperty
operator to get or set these properties.
这篇关于复选框检查/取消选中使用d3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!