取消选择多选一个选项中的所有选项

Sha*_*ded 3 html javascript internet-explorer-8

我目前有以下js代码

function clearMulti(option)
{
    var i;
    var select = document.getElementById(option.parentNode.id);
    for(i=1;i<select.options.length;i++)
         {
        select.options[i].selected=false;
    }
}
Run Code Online (Sandbox Code Playgroud)

function clearAllOpt(select)
{
    select.options[0].selected = false;
}
Run Code Online (Sandbox Code Playgroud)

第一个在调用时取消选择多个选择中的所有选项,第二个在选择任何其他选项时清除第一个选项.

需要的是第一个选项是All.

在FF中这一切都很好用,但是在IE8中没有任何反应......有关如何在两者中使用它的任何建议吗?

这是从一个jsp页面调用...下面的代码 - 编辑了如何填充id和事物,因为它的数据库信息和其他我可能不应该给出的东西:)但这应该给你的信息正在寻找.

<select id="blahSelect" name="blahSelect" style="width:80%" size=7 multiple>
     <option id="All Blah" onclick="clearMulti(this)">All Blah</option>
     <option id="**from sql**" onclick="clearAllOpt(this.parentNode)">**from sql**</option>
</select>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Dmi*_*try 6

愿这很容易

select.selectedIndex = -1;
Run Code Online (Sandbox Code Playgroud)

注意:值"-1"将取消选择所有选项(如果有).

资料来源:http://www.w3schools.com/jsref/prop_select_selectedindex.asp


gre*_*eim 1

不要为每个选项单独使用 onclick="",而是尝试在选择上使用 onchange="":

document.getElementById("bar").onchange=function(){
    if(this.options.selectedIndex > 0){
        /* deselect this.options[0] */
    }else{
        /* deselect this.options[1-n] */
    }
}
Run Code Online (Sandbox Code Playgroud)

在 HTML 中:

<select id="bar">
    <option>ALL</option>
    <option>option 1</option>
    <option>option 2</option>
    ...
    <option>option n</option>
</select>
Run Code Online (Sandbox Code Playgroud)