您现在的位置是:首页 > 电脑技术查询 > web开发

怎么通过javascript将listbox中的项全部选中?

编辑:chaxungu时间:2022-10-02 23:21:21分类:web开发

如何通过javascript将listbox中的项全部选中??
我有一个listbox,已经设置为可以多选了,但是现在不知道怎么通过javascript来选中其所有项,(因为不选中的话,该listbox传的值会为空)

我使用下面这个方法,虽然不报错,但是没有反应;
function selectall(){
var i;

for (i=0;i <=document.getElementById( "simpledistrict ").options.length;i++){
document.getElementById( "simpledistrict ").SelectedIndex=i;
}
}

------解决方案--------------------
for (vari=0;i <=document.getElementById( "simpledistrict ").options.length;i++)
{
document.getElementById( "simpledistrict ").options[i].selected = true;
}