jQuery 如何检查下拉菜单中有多少选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3337749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:15:57  来源:igfitidea点击:

How do I check how many options there are in a dropdown menu?

jquerydrop-down-menu

提问by Asim Zaidi

How do I check, using jQuery, how many options are there in a drop down menu?

如何使用 jQuery 检查下拉菜单中有多少选项?

Thanks.

谢谢。

回答by user113716

var length = $('#mySelectList').children('option').length;

or

或者

var length = $('#mySelectList > option').length;

This assumes your <select>list has an ID of mySelectList.

这假设您的<select>列表的 ID 为mySelectList

回答by Matti Virkkunen

$("#mydropdown option").length

Or if you already got a reference to it,

或者如果你已经得到了它的参考,

$(myDropdown).find("option").length

回答by Adam

Use the length propertyor the size methodto find out how many items are in a jQuery collection. Use the descendant selectorto select all <option>'s within a <select>.

使用length 属性size 方法找出 jQuery 集合中有多少项。使用后代选择,选择所有<option>内的<select>

HTML:

HTML:

<select id="myDropDown">
<option>1</option>
<option>2</option>
.
.
.
</select>

JQuery:

查询:

var numberOfOptions = $('select#myDropDown option').length

And a quick note, often you will need to do something in jquery for a very specific thing, but you first need to see if the very specific thing exists. The length property is the perfect tool. example:

一个快速说明,通常你需要在 jquery 中为一个非常具体的事情做一些事情,但你首先需要看看这个非常具体的事情是否存在。length 属性是完美的工具。例子:

   if($('#myDropDown option').length > 0{
      //do your stuff..
    } 

This 'translates' to "If item with ID=myDropDown has any descendent 'option' s, go do what you need to do.

这“转换”为“如果 ID=myDropDown 的项目具有任何后代 'option',请执行您需要执行的操作。

回答by Munzilla

Click here to see a previous post about this

单击此处查看之前关于此的帖子

Basically just target the ID of the select and do this:

基本上只是针对选择的 ID 并执行以下操作:

var numberOfOptions = $('#selectId option').length;

回答by Sharjeel Aziz

Get the number of options in a particular select element

获取特定选择元素中的选项数

$("#elementid option").length

回答by Rob Stevenson-Leggett

$('#idofdropdown option').length;

That should do it.

那应该这样做。

回答by fantactuka

alert($('#select_id option').length);

回答by nicholasklick

$('select option').length;

or

或者

$("select option").size()

回答by second

$('#dropdown_id').find('option').length

回答by Vatsal

With pure javascript you can just call the length on the id of the select box. It will be more faster. Typically with everything native javascript is performing better and better with modern browsers

使用纯 javascript,您只需在选择框的 id 上调用长度即可。它会更快。通常,原生 javascript 在现代浏览器中的性能越来越好

This can be achieved in javascript by

这可以在javascript中通过

     var dropdownFilterSite = document.querySelector( '#dropDownId' );  //Similar to jQuery

var length = dropdownFilterSite.length.

Good website for some learning

一些学习的好网站

www.youmightnotneedjquery.com

www.youmightnotneedjquery.com

A good video to watch by Todd Motto

托德·莫托(Todd Motto)观看的好视频

https://www.youtube.com/watch?v=pLISnANteJY

https://www.youtube.com/watch?v=pLISnANTeJY