Is it possible to test how many options are in a select box?
我正在使用 jQuery 1.6.2.
我正在使用 ColdFusion 和 jQuery 动态填充选择框。
When the options are returned and stuffed inside the select box, I'd jQuery to figure out how many options there are and adjust the size attr as needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 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 ); |
如何有效地获得返回的选项数量?
是的,只需使用
1 | var numOfOptions = $('#Select option').length; |
只需选择选项并测试生成的 jQuery 对象的长度属性:
1 | console.log($("#Select option").length); |
你可以使用这个:
1 | var NumOfOptions = $("#AlbumsSelect option").length; |
尝试使用javascript的属性.length
1 | var length = $("option").length; |