javascript - getElementById selected 不能选择多个选项

标签 javascript html select getelementbyid

我在 javascript 中有一个函数应该允许我选择多个选项,但它只选择第一个。

Javascript:

 function index(){
        var a="${staffindex}".replace("[","");
        var b=a.replace("]","");
        var index1=b.split(",");
        for(var i=0;i<index1.length;i++)
            document.getElementById("Staff")[index1[i]].selected=true;
        }

HTML:

<select multiple="multiple" id="Staff" name=staff>
                <option value = "1" >option1</option>
        <option value = "2" >option2</option>
        <option value = "3" >option3</option>
        <option value = "4" >option4</option>
        <option value = "5" >option5</option>
</select>

index1 是从 java 类接收的数字数组。
感谢您的帮助!

最佳答案

你忘记使用options数组:

document.getElementById("Staff").options[index1[i]].selected=true;

关于javascript - getElementById selected 不能选择多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047197/

相关文章:

javascript - 如何在 ng-repeat angularjs 中传递多个 $scope 值?

javascript - 使用模板编译器处理文件时(针对目标 web.browser)

php - Mysql 连接两个表的 where 子句

javascript - 使用 ramda 生成查询字符串

javascript - iOS 上的 Phonegap fileReader 错误消息

javascript - Flexbox 换行仅第一列带有溢出滚动

javascript - 输入 "change"事件是否保证在表单 "submit"之前运行?

jquery - 数据表:使用表外的链接进行过滤

mysql - 如何从 MySQL 中另一个 select 的结果中进行选择?

mysql - 在 MySql 中,如何将数据插入到引用其他三个表的表中,然后选择该数据?