0

所以我希望能够隐藏所有选项,然后只显示那些不包含hello_在其价值中的选项。这包括hello_byehello_hello任何以hello_

这是我到目前为止所拥有的:

jQuery(document).ready(function(){
    jQuery("#metakeyselect > option").hide();
    jQuery("#metakeyselect > option[//what goes here?//]").show();
});

如何显示包含 hello_ 在内的值的所有 BUT 选项?

4

3 回答 3

4

hello_您可以使用attribute-starts-with选择器“隐藏”其值以开头的那些。

正如Alex正确指出的那样,并非所有浏览器都允许您隐藏选项元素(另请参阅如何隐藏 optgroup/选项元素?)。但是您可以删除它们:

var hidden_options = jQuery("#metakeyselect > option[value^=hello_]").remove();

或禁用它们:

jQuery("#metakeyselect > option[value^=hello_]").prop('disabled', true);

取决于您还想对它们做什么。

于 2013-06-21T21:45:50.617 回答
3

http://api.jquery.com/contains-selector/

包含的元素hello_

$("#metakeyselect > option:contains('hello_')")

不包含的元素hello_

$("#metakeyselect > option:not(:contains('hello_'))")
于 2013-06-21T21:43:44.903 回答
0

您可以使用:

$(document).ready(function(){
    var options = $('#metakeyselect > option');
    var contains = "hello_";

    options.each(function() {
        if($(this).val().indexOf(contains) != -1){
            $(this).remove();
        }
    });
});

或者

$(document).ready(function(){
    $("#metakeyselect > option").remove();
    $("#metakeyselect > option[value*='hello_']").show();
});

您将需要使用 remove() 作为选项。

于 2013-06-21T22:22:11.977 回答