我正在使用此函数将选定的选项输出到滚动列表中的警报:
var topping = "";
function toppingTotal() {
var top = document.getElementById('topList');
if (top.selectedIndex > 0) {
topping = topping + "Toppings: " + top.options[top.selectedIndex].text;
return topping;
}
}
代码工作正常,但如何让它输出多个选择?
最佳答案
我假设您有一个带有属性multiple
的选择。你可以试试这个
for (var i = 0; i < top.options.length; i++) {
if (top.options[i].selected) {
topping += top.options[i].text + " ";
}
}
return topping;
关于javascript - 如何让这个函数读取多个选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33975664/