jQuery 使用jQuery,如何动态设置选择框的大小属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/65206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:41:20  来源:igfitidea点击:

Using jQuery, how can I dynamically set the size attribute of a select box?

jquery

提问by Jay Corbett

Using jQuery, how can I dynamically set the size attribute of a select box?

使用jQuery,如何动态设置选择框的大小属性?

I would like to include it in this code:

我想将它包含在此代码中:

$("#mySelect").bind("click",
    function() {
        $("#myOtherSelect").children().remove();
        var options = '' ;
        for (var i = 0; i < myArray[this.value].length; i++) {
            options += '<option value="' + myArray[this.value][i] + '">' + myArray[this.value][i] + '</option>';
        }
        $("#myOtherSelect").html(options).attr [... use myArray[this.value].length here ...];
    });
});

回答by Loren Segal

Oops, it's

哎呀,是

$('#mySelect').attr('size', value)

回答by Jay Corbett

$("#mySelect").bind("click", function(){
    $("#myOtherSelect").children().remove();
    var myArray = [ "value1", "value2", "value3" ];
    for (var i = 0; i < myArray.length; i++) {
        $("#myOtherSelect").append( '<option value="' + myArray[i] + '">' + myArray[i] + '</option>' );
    }
    $("#myOtherSelect").attr( "size", myArray.length );
});