如何设置< 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引用而没有在select元素上指定“myselect”的id,Albert的注释也是正确的.