我使用的是 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/