开发者

jquery select remove option

I'm currently using this successfully to remove an 开发者_运维知识库option

    $("select#select_gender option[value='initial']").remove();

Is there a way remove an option without adding to the selector - like below?

    $('select#select_gender').val('initial').remove();

thx


$("select#select_gender option").filter("[value='initial']").remove();

I believe that does it.

or per your latest comment:

var sel = $("select#select_gender");
sel.find("option[value='initial']").remove();

PS sorry for so many edits :(


You could use something like this:

$("#select_gender").children().filter(function(index, option) {
    return option.value==="initial";
}).remove();

If you wanted, you could turn it into a plugin, like so:

;(function($) {
    $.fn.option=function(value) {
        return this.children().filter(function(index, option) {
            return option.value===value;
        });
    };
})(jQuery);

Then you could use this:

$("#select_gender").option("initial").remove();

You can demo it here.


I suppose you can restrict it down to the option tags

$("select#select_genter option").find("[value='initial']").remove()

and then for 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();
}


Why would you do it? You can do something like

$('select#select_gender').each(function(a,b){
    if ($(this).val() == 'initial'){
       $(this).remove();
    }
});

but I DO NOT RECOMMEND IT. Use that first one. It's perfect and easy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜