取消选择多选一个选项中的所有选项 [英] Deselect all options in Multiple Select with 1 option

查看:184
本文介绍了取消选择多选一个选项中的所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下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;
    }
}

function clearAllOpt(select)
{
    select.options[0].selected = false;
}

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

The first one deselects all options in the multiple select when called and the second clears the first option whenever anything else is selected.

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

The need for this is that the first option is for All.

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

This all works fine and dandy in FF, but in IE8 nothing happens... any suggestions on how to get this to work in both?

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

This is called from a jsp page... code below -- edits were made for how ids and things are populated since it's database info and other things that I probably shouldn't give out :) but this should give you the info that you're looking for.

<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>

提前致谢。

推荐答案

对于每个选项而不是单独的onclick =,请在选择中尝试onchange =:

Instead of a separate onclick="" for each option, try an onchange="" on the select:

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

并在HTML中:

<select id="bar">
    <option>ALL</option>
    <option>option 1</option>
    <option>option 2</option>
    ...
    <option>option n</option>
</select>

这篇关于取消选择多选一个选项中的所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆