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

标签 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 NumOfOptions = $();
var BoxSize = 0;

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

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

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

最佳答案

是的,只需使用length属性...

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

关于javascript - 是否可以测试选择框中有多少个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501004/

相关文章:

javascript - 使用asp.net创建的js或jquery读取cookie

javascript - 使用 jQuery 禁用/启用输入?

javascript - 仅在表单值发生更改时发送 post/get

javascript - 无法使用 JavaScript 更新网站图标以使其旋转

javascript - Meteor 中的用户更新页面

javascript - Kendo UI 多选框将 kendo.all.js 更新到最新版本

javascript - 尝试在 ReactJS 中针对对象内、对象内的 JSON 对象...

javascript - 使用 jQuery 固定菜单和滚动到 DIV

jQuery Droppables - 隐藏 'inactive' 放置区域时出现问题

javascript - 找到所选元素的最近链接