简体   繁体   English

如何确定下拉列表是否有可供选择的选项?

[英]How to tell if a drop down has options to select?

如何确定下拉列表是否有可供选择的选项?

var menu = getElementById("select_id");
if(menu.options.length) {
    // has children
} else {
    // empty
}
if ($("#myselect option").length > 0) {
  // Yay we have options
}
var hasOptions = !!$('#theSelect option').filter(function() { return !this.disabled; }).length;

maybe? 也许? This looks for <option> elements that are not disabled. 这将查找未禁用的<option>元素。

$('#input1 option').length > 0

其中#input是您正在运行此测试的select元素的ID。

if ($("#myselect option:enabled").length){
   // Yay!
}else{
   // Oh, no available options
}

http://api.jquery.com/enabled-selector/ http://api.jquery.com/enabled-selector/

Native javascript solution: 原生javascript解决方案:

!!document.getElementById('jiveviewthreadsform-filter').children.length

(Please do not overuse jQuery, thanks) (请不要过度使用jQuery,谢谢)

Very hackily, you can just check its selectedIndex; 非常hackily,你可以检查其selectedIndex; since most browsers ensure that there is something selected if at all possible, it will only be -1 if there are no selectable options. 由于大多数浏览器确保尽可能选择某些内容,但如果没有可选选项,则只会为-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM