JQUERY如果包含特定值,则自动隐藏SELECT LIST

时间:2013-01-18 23:59:23

标签: jquery

<select name="productsize" id="productsize">
    <option value="">1</option>
    <option value="">xxx</option>
</select>

如果jQuery包含“xxx”作为选项之一,那么jQuery如何隐藏此SELECT列表?

有可能吗?

UPDATE:

以上是一个例子,ROB回答它,它确实有效。但在我的情况下,这个选择列表首先由另一个jquery函数生成。

就是这样:

<script type="text/javascript"><!--
function GetAvailProductSizes() {
    $('select#productsize option').remove();
    $('select#productsize').append('<option value=""><? echo $langdata['oneprodpage_selectsize']; ?>...</option>');

    var color = $('#productcolor').val();
    if (color > 0) {
        var availsizes;
        var http_request = new XMLHttpRequest();
        http_request.open( "GET", '<? echo ROOT; ?>/autocompleteavailsizes/?productid=<? echo $thisproduct['id']; ?>&color=' + color, true );
        http_request.send(null);
        http_request.onreadystatechange = function () {
            if ( http_request.readyState == 4 ) {
                if ( http_request.status == 200 ) {
                    availsizes = eval( "(" + http_request.responseText + ")" );
                    $('select#productsize').show();

                    for (var i = 0; i < availsizes.length; i++) {
                        $('select#productsize').append('<option value="' + availsizes[i].id + '">' + availsizes[i].name + '</option>');
                    };
                } else {
                    alert( "There was a problem with the URL." );
                }
                http_request = null;
            }
        };
    };
}

// - &GT;

我试过这个:

<script type="text/javascript"><!--
function GetAvailProductSizes() {
    $('select#productsize option').remove();
    $('select#productsize').append('<option value=""><? echo $langdata['oneprodpage_selectsize']; ?>...</option>');

    var color = $('#productcolor').val();
    if (color > 0) {
        var availsizes;
        var http_request = new XMLHttpRequest();
        http_request.open( "GET", '<? echo ROOT; ?>/autocompleteavailsizes/?productid=<? echo $thisproduct['id']; ?>&color=' + color, true );
        http_request.send(null);
        http_request.onreadystatechange = function () {
            if ( http_request.readyState == 4 ) {
                if ( http_request.status == 200 ) {
                    availsizes = eval( "(" + http_request.responseText + ")" );
                    $('select#productsize').show();
                    $('select#productsize option').each(function() {
                        if ($(this).text() == '-') {
                            $('select#productsize').hide();
                        }
                    });
                    for (var i = 0; i < availsizes.length; i++) {
                        $('select#productsize').append('<option value="' + availsizes[i].id + '">' + availsizes[i].name + '</option>');
                    };
                } else {
                    alert( "There was a problem with the URL." );
                }
                http_request = null;
            }
        };
    };
}

// - &GT;

但它不起作用......

哦是的,我的选择列表的样式为 display:none

3 个答案:

答案 0 :(得分:2)

相当直接......

$('#productsize option').each(function() {
    if ($(this).text() == 'xxx') {
        $('#productsize').hide();
    }
});

http://jsfiddle.net/uRAsu/

答案 1 :(得分:2)

$('select:has(option:contains("xxx"))').hide();

或者:

$('#productsize option').filter(function(){
   var t = this.textContent || this.innerText;
   return t === 'xxx'; 
}).parent().hide();

http://jsfiddle.net/Vz7gY/

答案 2 :(得分:1)

Rob的回答的替代方法是检查从此选择器中选择的元素数量。

if($('#productsize option[value="xxx"]').length) {
    $('#productsize').hide();
}

这假定您设置每个选项的值属性而不是内部文本。