Html 如何取消选择选择框中的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6876174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:48:51  来源:igfitidea点击:

How to unselect an item in a select box?

htmlselect

提问by Evik James

I have an easy question for you today. I have a select box that enables the user to select multiple items. Also, when the user returns to the page, he can see which items he selected and unselect them if necessary. The trouble is that when I try to unselect, the selection just turns gray; it does NOT unselect.

我今天有一个简单的问题要问你。我有一个选择框,可以让用户选择多个项目。此外,当用户返回页面时,他可以看到他选择了哪些项目,并在必要时取消选择它们。麻烦的是,当我尝试取消选择时,选择只会变成灰色;它不会取消选择。

<select name="MySelect" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>

So, when the element above renders to the page, I have no way to unselecting the option. The value is STILL getting passed to the form.

因此,当上面的元素呈现到页面时,我无法取消选择该选项。该值仍在传递给表单。

Any help?

有什么帮助吗?

回答by Joseph Marikle

You can deselect options in a multi-value select list by ctrl-clicking it.

您可以通过按住 ctrl 单击来取消选择多值选择列表中的选项。

回答by Alessandro Battistini

You can use JS object to memorize the status of a row and click action on select (JQuery).

您可以使用 JS 对象来记住行的状态并单击选择(JQuery)上的操作。

$('#yourselect').click( function(e){        
        var i = this.selectedIndex;
        if(this.options[i].selectedOld==true){
            this.options[i].selected = false;
            this.options[i].selectedOld = false;
        }
        else{
            this.options[i].selectedOld = true;
        }
    });