最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove items multi select with optgroup in jquery - Stack Overflow

programmeradmin0浏览0评论

Is there a way to clear all option and optgroup HTML elements by using jquery?

My HTML select element is like this,

<select id="users" name="multiselect" multiple="multiple" style="width:382px;" >
    <optgroup id="groupadmin" label="Group Admin"></optgroup>
    <optgroup id="systemusers" label="System User"></optgroup>
</select>

Is there a way to clear all option and optgroup HTML elements by using jquery?

My HTML select element is like this,

<select id="users" name="multiselect" multiple="multiple" style="width:382px;" >
    <optgroup id="groupadmin" label="Group Admin"></optgroup>
    <optgroup id="systemusers" label="System User"></optgroup>
</select>
Share Improve this question edited Jun 26, 2014 at 12:08 Ja͢ck 174k39 gold badges266 silver badges314 bronze badges asked Jun 26, 2014 at 11:47 KanishkaKanishka 232 silver badges7 bronze badges 2
  • Yes there is; have you tried anything? – Ja͢ck Commented Jun 26, 2014 at 11:58
  • I have tried following ways, $('#users').empty(); $('#users').find('optgroup,option').remove(); $('#users').html($('#users').html().replace('selected','')); – Kanishka Commented Jun 26, 2014 at 12:06
Add a ment  | 

3 Answers 3

Reset to default 6

You can use .empty()

Description: Remove all child nodes of the set of matched elements from the DOM.

Code

$('#users').empty()

Fiddle

EDIT

As you are using multiSelect plugin, You need to use .multiSelect('refresh') method.

$('#users').empty().multiSelect('refresh'); 

Updated Fiddle

Try this code

   $('#users').find('optgroup,option').remove();

Use the following code :

$('#users').children().remove();

This will work irrespective of whether an optgroup is present or not

发布评论

评论列表(0)

  1. 暂无评论