11

使用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 ...];
    });
});
4

2 回答 2

25

糟糕,是

$('#mySelect').attr('size', value)
于 2008-09-15T18:06:34.613 回答
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 );
});
于 2008-09-15T18:09:38.537 回答