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

时间:2008-09-15 18:02:10

标签: jquery

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

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

$("#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 ...];
    });
});

2 个答案:

答案 0 :(得分:24)

哎呀,这是

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

答案 1 :(得分:1)

$("#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 );
});