从select中删除空选项

时间:2014-07-17 12:29:58

标签: jquery html5

我有这种HTML

<select id="SelectedCulture" name="SelectedCulture">
<option></option>
<option>en-US</option>
</select>

我在Jquery中需要删除的是空的吗?

3 个答案:

答案 0 :(得分:6)

您可以使用:empty检查它是否为空。然后使用remove()删除元素。

$("#SelectedCulture option:empty").remove();

Fiddle Demo

答案 1 :(得分:1)

去吧:

$("#SelectedCulture > option").each(function(){
    if($(this).text().length==0)
        $(this).remove();
});

DEMO

答案 2 :(得分:1)

$("option").each(function() {
    var text= $(this).text();
    if(text== '') { // or anything else you want to remove...
        $(this).remove();
    }
});

这将删除

中的空元素

<强> [编辑]

仍然建议更喜欢Anoop Joshi的回答