How to clear an ASP.NET listbox with Javascript?

这一生的挚爱 提交于 2019-12-23 20:26:59

问题


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



来源:https://stackoverflow.com/questions/6736954/how-to-clear-an-asp-net-listbox-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!