我正在使用此功能从滚动列表中向警报输出所选选项:
var topping = "";
function toppingTotal() {
var top = document.getElementById('topList');
if (top.selectedIndex > 0) {
topping = topping + "Toppings: " + top.options[top.selectedIndex].text;
return topping;
}
}
代码工作正常,但如何让它输出多个选择?
答案 0 :(得分:0)
我假设您选择了属性multiple
。你可以试试这个
for (var i = 0; i < top.options.length; i++) {
if (top.options[i].selected) {
topping += top.options[i].text + " ";
}
}
return topping;