我正在努力学习D3.是否有一种优雅的方法来搜索选择表单元素中的所有选项,并指定一个匹配值为"已选中"的选项?
var xStat = "G";
var statOptions = {
"Points": "PTS",
"Goals": "G",
"Assists": "A",
"Penalty Minutes": "PIM"
};
// create the select element
var selectUI = d3.select("#horse").append("form").append("select");
// create the options
selectUI.selectAll("option").data(d3.keys(statOptions)).enter().append("option").text(function(d) {
return d;
});
// add values to the options
selectUI.selectAll("option").data(d3.values(statOptions)).attr("value", function(d) {
return d;
});
// create a func to check if the value of an option equals the xStat var
// if yes, set the attribute "selected" to "selected"
var checkOption = function(e, …Run Code Online (Sandbox Code Playgroud)