我有一个下拉列表,我想清除它使用jquery的所有项目。我看到很多关于删除选定项目的谷歌链接,但我想从下拉列表中清除所有项目。使用jquery从选择下拉列表中删除所有条目的最简单方法是什么?
从选择下拉列表中删除所有项目的最佳方式是什么?
我有一个下拉列表,我想清除它使用jquery的所有项目。我看到很多关于删除选定项目的谷歌链接,但我想从下拉列表中清除所有项目。使用jquery从选择下拉列表中删除所有条目的最简单方法是什么?
从选择下拉列表中删除所有项目的最佳方式是什么?
$('#idOfDropdown option').remove();
$('option', the_select_element).remove();
如果你想保留选择:
$('option:not(:selected)', the_select_element).remove();
这是纯JS真不简单,太(感谢,@Austin法国!):
// get the element
var sel = document.getElementById("the_select_ID");
// as long as it has options
while (sel.options.length) {
// remove the first and repeat
sel.remove(0);
}
简单的JS方法要做到这一点:var options = el.options; while(options.length)options [0] .remove(); – 2014-01-22 14:57:59
它有点不同:'while(select_element.options.length)select_element.remove(0);'但是。感谢提及本地界面! – Boldewyn 2014-01-22 15:09:13
甚至没有考虑到这一点。绝对是一个更好的方式来做到这一点。 – 2011-05-27 13:02:09
是的。 '空()'是要走的路。 +1 – Boldewyn 2011-05-27 13:04:26