Javascript 是否可以测试一个选择框中有多少个选项?

Javascript 是否可以测试一个选择框中有多少个选项?,javascript,jquery,Javascript,Jquery,我使用的是jQuery 1.6.2 我使用ColdFusion和jQuery动态填充一个选择框 当返回选项并将其填充到选择框中时,我会使用jQuery计算出有多少个选项,并根据需要调整大小属性 // load the options into the select box $("#Select").load("GlobalAdmin/ArtistUnassociatedAlbums.cfm?" + QString); // test the number of options var NumO

我使用的是jQuery 1.6.2

我使用ColdFusion和jQuery动态填充一个选择框

当返回选项并将其填充到选择框中时,我会使用jQuery计算出有多少个选项,并根据需要调整大小属性

// load the options into the select box
$("#Select").load("GlobalAdmin/ArtistUnassociatedAlbums.cfm?" + QString);

// test the number of options
var NumOfOptions = $();
var BoxSize = 0;

// do a calculation
if (NumOfOptions > 10) {
    BoxSize = 10;
} else {
    BoxSize = NumOfOptions ;
}

// adjust the select box size
$("#AlbumsSelect").attr("size", BoxSize );

如何有效地获取返回的选项数?

只需选择选项并测试结果jQuery对象的length属性:

console.log($("#Select option").length);

是的,只需使用
length
属性

var numOfOptions = $('#Select option').length;

尝试使用javascript的属性.length

var length = $("option").length;

您可以使用以下功能:

var NumOfOptions = $("#AlbumsSelect option").length;

你能停止在你所有的标题中写标签吗?什么是“最新和最好的jQuery”?这句话取决于某个时间点,但这个问题帖子将持续[可能]年。准确地告诉我们您使用的版本;不会痛的!可能是@Felix的复制品,我的问题更明确。另一个问题更具概念性。另外,那个家伙在他的问题中加入了“jQuery”,应该受到警告,就像我一样。:>@埃维克·詹姆斯,没问题。很高兴我能帮上忙。我会很乐意尽快帮上忙。我试过了,但我想至少有十分钟。是的,这也很完美。谢谢