Javascript 如何在下拉事件更改时使用 jquery 清除或清空列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11294554/
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-24 05:20:34  来源:igfitidea点击:

how to clear or Empty list box using jquery on dropdown event change

javascriptjqueryjquery-ui

提问by user937194

Can any body help me out how to clear the items in the list box on dropdown event change.

任何机构都可以帮助我了解如何在下拉事件更改时清除列表框中的项目。

$(function () {
        $("#ddlLevelColumn").change(function () {
            $("#lstCodelist") ------ I need to clear this listbox1
            $("#lbxSelectedItems")--------------- need to clear list box 2

        });
    });

 <%:Html.ListBox("lstCodelist", Model.CodeListDefaultValue, new { style = "width:99%;height:297px;" })%>
<%:Html.ListBox("lbxSelectedItems", Model.AffectedCodeListboxData, new { style = "width:99%;height:297px;color:blue;" })%>

Thanks for your help..

谢谢你的帮助..

回答by ZeeCoder

empty()

空的()

$("#lstCodelist").empty()
$("#lbxSelectedItems").empty()

回答by Brad Christie

You can remove all entries (or apply filters as well):

您可以删除所有条目(或也可以应用过滤器):

$('#listBoxId > option').remove();           // all options
$('#listBoxId > option[val!=""]').remove(); // keep non-empty values

That what you're going for? I believe even simpler:

那你去干嘛?我相信更简单:

$('#listBoxId').empty();

Should work as well.

也应该工作。

Working Demo: http://jsfiddle.net/jEWe6/

工作演示:http: //jsfiddle.net/jEWe6/

回答by billyonecan

I think .empty()is what you're looking for.

我想.empty()这就是你要找的。

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

从 DOM 中删除匹配元素集的所有子节点。

http://api.jquery.com/empty/

http://api.jquery.com/empty/

回答by Adrien E

You can clear select html elements with something like this:

您可以使用以下内容清除选择的 html 元素:

var clear = function() {
  $("#lstCodelist").empty().append('<option value="whatever">Wait for reload</option>');
  $("#lbxSelectedItems").empty().append('<option value="whatever">Wait for reload</option>');
});

回答by Mitul Maheshwari

you can also try any of this two approach.

您也可以尝试这两种方法中的任何一种。

  1. $('#RolesListAvailable').html('');
  1. $('#RolesListAvailable').html('');

OR

或者

  1. $('#RolesListAssigned').empty();
  1. $('#RolesListAssigned').empty();

回答by Akhil

Try this....

尝试这个....

$('#listBoxId').empty();