d3.js - 在 D3 中设置 <select> 元素的值?

标签 d3.js

如何设置 <select> 的值元素,使用 D3.js?

我正在尝试这个:

d3.select('#myselect').attr('value', 'France');

但它不起作用。

JSFiddle在这里:http://jsfiddle.net/06z9tnzs/

最佳答案

您需要使用 属性(property)而不是 attr 用于设置值。
d3.select('#myselect').property('value', 'France');
从 d3 文档:

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 a checked boolean property. You can use the property operator to get or set these properties, or any other addressable field on the underlying element, such as className.



https://github.com/mbostock/d3/wiki/Selections#property

关于您的 JSFiddle 缺少 d3.js 引用并且没有在 上指定“myselect”的 id,Albert 的评论也是正确的。选择 元素。

关于d3.js - 在 D3 中设置 <select> 元素的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28933727/

相关文章:

javascript - d3.js 基于标签文本的大小节点

javascript - twitter bootstrap 选项卡和 getBBox

javascript - 通过采样/插值减少大型数据集的大小以提高图表性能

javascript - 如何垂直制作D3 Streamgraph

javascript - for...in 语句 - 任意顺序是什么意思

javascript - 创建动态全屏和最小化 Div 函数

arrays - 访问多维 D3 Nest() 汇总键和值

d3.js - 影响 d3js mouseout 事件的延迟转换

javascript - 将分类数据的配色方案与线性范围相结合

javascript - 如何在 Rails 应用程序中显示 d3.js 圆包图?