小编ssp*_*oyd的帖子

如何使用D3(或只是javascript)将表单选项设置为"已选中"

我正在努力学习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)

html javascript forms option d3.js

5
推荐指数
1
解决办法
5502
查看次数

标签 统计

d3.js ×1

forms ×1

html ×1

javascript ×1

option ×1