How to clear an ASP.NET listbox with Javascript?

2019-07-01 15:42发布

问题:

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

回答1:

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

or

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


回答2:

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