简体   繁体   中英

How to clear an ASP.NET listbox with Javascript?

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

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

does not work.

Any ideas would be greatly appreciated!

Thanks,

Jason

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

or

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

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 = "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM