如何确定下拉列表是否有可供选择的选项?
如何确定下拉列表是否有可供选择的选项?
var hasOptions = !!$('#theSelect option').filter(function() { return !this.disabled; }).length;
也许? 这将查找未禁用的元素。
var menu = getElementById("select_id"); if(menu.options.length) { // has children } else { // empty }
if ($("#myselect option").length > 0) { // Yay we have options }
$('#input1 option').length > 0
其中#input
是您正在运行此测试的select
元素的ID。
if ($("#myselect option:enabled").length){ // Yay! }else{ // Oh, no available options }
原生javascript解决方案:
!!document.getElementById('jiveviewthreadsform-filter').children.length
(请不要过度使用jQuery,谢谢)
非常hackily,你可以检查其selectedIndex; 由于大多数浏览器确保尽可能选择某些内容,但如果没有可选选项,则只会为-1。