如何设置 <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/