jQuery选择列表删除所有选项

时间:2009-07-09 15:32:48

标签: javascript jquery select firebug

我有一个<select>列表,其中已填充了多个选项,但想要删除这些选项以重新开始。

我正在使用jQuery并尝试了以下内容:

$("#selectId").length = 0;

但这似乎没有效果。

我的部分问题是我使用Firebug来调试JavaScript,但调试器不会在断点处中断,所以我看不到发生了什么。如果JavaScript在我的HTML文件的<head>范围内,它是否会中断?

7 个答案:

答案 0 :(得分:49)

这样做:

$('#selectId').html('');

答案 1 :(得分:28)

Just $("#selectId option").remove();也可以。

答案 2 :(得分:11)

$("#selectId").empty();适合我。

$("#selectId option").remove();从DOM中删除了选择。

目前正在使用jQuery JavaScript Library v1.7.1

答案 3 :(得分:6)

The fastest way to accomplish this:

powershell_script

Here are some tests: http://jsperf.com/find-remove-vs-empty

I take no credit for these tests. See this stack overflow question: How do you remove all the options of a select box and then add one option and select it with jQuery?

答案 4 :(得分:1)

如果没有jquery:

javascript SELECT remove()

答案 5 :(得分:1)

$("#selectId").options.length = 0;

这实际上不会像写的那样工作,因为它使用的是jQuery选择器。但是,如果你在选择列表中使用了直接的DOM getElementById,它会工作。无论是AKX还是d。的回答都会让你正确。

答案 6 :(得分:0)

尝试“多项选择”下拉菜单。

$('#FeatureId')。multiselect(“ deselectAll”,false).multiselect(“ refresh”);

相关问题