如何使用 Javascript 清除 ASP.NET 列表框?

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

How to clear an ASP.NET listbox with Javascript?

javascriptasp.netlistboxclear

提问by hungryMind

I'm trying to clear all of the items in an ASP.NET listbox via javascript, but sadly

我正在尝试通过 javascript 清除 ASP.NET 列表框中的所有项目,但遗憾的是

document.getElementById("<%= lstNames.clientID %>").items.clear;

does not work.

不起作用。

Any ideas would be greatly appreciated!

任何想法将不胜感激!

Thanks,

谢谢,

Jason

杰森

回答by hungryMind

Use jquery document.getElementById("<%= lstNames.clientID %>").empty()

使用jQuery document.getElementById("<%= lstNames.clientID %>").empty()

or

或者

document.getElementById("<%= lstNames.clientID %>").options.length = 0;

回答by Mrchief

Use:

利用:

document.getElementById("<%= lstNames.clientID %>").selectedIndex = -1; // will throw error on next if there are selected items
document.getElementById("<%= lstNames.clientID %>").options.length = 0;

or

或者

document.getElementById("<%= lstNames.clientID %>").innerHTML = "";

document.getElementById("<%= lstNames.clientID %>").innerHTML = "";