You need to add the .on("change")
to the select
element, not the option
elements.
var select = d3.select("#shru").append("select").on("change", change),
options = select.selectAll('option').data(dd); // Data join
// Enter selection
options.enter().append("option").text(function(d) { return d.teamShotID; });
The currently selected option
index is kept in a property called selectedIndex
on the select
element. Selections are arrays, so elements can be accessed directly (e.g., selection[0][0]
). Each option
element will have data bound to it, stored in a property called __data__
:
function change() {
var selectedIndex = select.property('selectedIndex'),
data = options[0][selectedIndex].__data__;
}
Edit: For readability and hopefully to help you understand the code above, I'd like to also include this alternative syntax:
function change() {
var si = select.property('selectedIndex'),
s = options.filter(function (d, i) { return i === si }),
data = s.datum();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…