javascript - De-select all html select options in a control that does not allow multiple selection - Stack Overflow

I have a web page with several HTML Select controls that all allow multiple selection.Next to each co

I have a web page with several HTML Select controls that all allow multiple selection. Next to each control we have a "Clear" button that de-selects all selected items in the control, with the onclick that looks something like this:

<input type=button value="Clear" size=5 onclick="selectOptions('n.MyControl', false)">

The called Javascript code looks like this:

function selectOptions(controlName, bSelectItems)
{
    for (i=0; i < document.myForm[controlName].options.length; i++)
    {
        document.myForm[controlName].options[i].selected = bSelectItems;
    }

}

It works really well with the Select controls that allow multiple selection, but I just added a Select control that does not allow multiple selection (i.e. does not have the "multiple" attribute), and this code does not clear it. If I add the "multiple" attribute, the Clear button starts working again, so I know it has something to do with the fact that I am not allowing multiple items to be selected.

My question is, why does the above code not work and more importantly, how can I get my new Select control cleared in JavaScript?

I have a web page with several HTML Select controls that all allow multiple selection. Next to each control we have a "Clear" button that de-selects all selected items in the control, with the onclick that looks something like this:

<input type=button value="Clear" size=5 onclick="selectOptions('n.MyControl', false)">

The called Javascript code looks like this:

function selectOptions(controlName, bSelectItems)
{
    for (i=0; i < document.myForm[controlName].options.length; i++)
    {
        document.myForm[controlName].options[i].selected = bSelectItems;
    }

}

It works really well with the Select controls that allow multiple selection, but I just added a Select control that does not allow multiple selection (i.e. does not have the "multiple" attribute), and this code does not clear it. If I add the "multiple" attribute, the Clear button starts working again, so I know it has something to do with the fact that I am not allowing multiple items to be selected.

My question is, why does the above code not work and more importantly, how can I get my new Select control cleared in JavaScript?

Share Improve this question edited Sep 21, 2010 at 18:09 Patrick asked Sep 21, 2010 at 17:21 PatrickPatrick 6,0204 gold badges26 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The reason your code doesn't work is because you are trying to iterate over each option and do something, but since (like you said) the multiple attribute isn't set, it doesn't let you modify multiple options.

As for fixing it, you can try to get the selected option and then just deselect that single option.

Update: I don't know for sure that this is the ideal solution, but it seems to work for me in IE8.

document.getElementById('myselect').selectedIndex = -1;

Of course you can get the select element whichever way works best for you, but just set the selectedIndex attribute to -1.

There is no such thing as a cleared single-<select> box. One option will always be selected as long as there are any options in the select at all. If no options have selected set at first load time, the first option will automatically get selected.

The usual approach is to have the first option as a dummy-option like <option value="">(Please select a thing)</option>, and reset to that (with select.selectedIndex= 0).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745218571a4617139.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信