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

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

是否可以?

更新:

以上只是一个例子,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;
            }
        };
    };
}

//-->

我试过这个:

<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;
            }
        };
    };
}

//-->

但它不工作......

哦,是的,我的选择列表有样式显示:无

4

3 回答 3

2

非常坦率的...

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

http://jsfiddle.net/uRAsu/

于 2013-01-19T00:02:07.530 回答
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/

于 2013-01-19T00:05:00.277 回答
1

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

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

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

于 2013-01-19T00:08:12.773 回答