jquery选择删除选项
我目前正在成功使用它来删除一个选项
$("select#select_gender option[value='initial']").remove();
有没有办法删除选项而不添加到选择器 – 如下所示?
$('select#select_gender').val('initial').remove();
谢谢
$("select#select_gender option").filter("[value='initial']").remove();
我相信这样做。
或根据您的最新评论:
var sel = $("select#select_gender"); sel.find("option[value='initial']").remove();
PS抱歉这么多的编辑:(
你可以使用这样的东西:
$("#select_gender").children().filter(function(index, option) { return option.value==="initial"; }).remove();
如果你想,你可以将它变成一个插件,如下所示:
;(function($) { $.fn.option=function(value) { return this.children().filter(function(index, option) { return option.value===value; }); }; })(jQuery);
然后你可以使用这个:
$("#select_gender").option("initial").remove();
你可以在这里演示它。
我想你可以将它限制在选项标签上
$("select#select_genter option").find("[value='initial']").remove()
然后是diffenet vals
var beg_string = "[value='", end_string = "']", while ( ) { /* loop through a list of values */ $("select#select_genter option").find(beg_string + val + end_str).remove(); }
你为什么要这样做? 你可以做点什么
$('select#select_gender').each(function(a,b){ if ($(this).val() == 'initial'){ $(this).remove(); } });
但我不推荐它 。 使用第一个。 这是完美而简单的