JavaScript-获取列表选项的长度 [英] JavaScript - get length of list options

查看:685
本文介绍了JavaScript-获取列表选项的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在选择下拉HTML输入标签中获得列表选项的长度?列表是动态变化的,因此我需要计算下拉菜单中的选项总数。总选项是动态生成的,因此我需要一种方法来计算html select标签中的选项标签数量。我还需要在纯JS中执行此操作,因为正在使用的应用程序不允许我使用JQuery。 (请不要与我争论,除非绝对没有办法用纯JS做到这一点。)

Is there a way I can get the length of list options in a select-dropdown HTML input tag? The list changes dynamically and I need to calculate the total number of options within the drop-down. The total options are generated dynamically, so I need a way to calculate number of option tags within an html select tag. I also need to do this in pure JS because the app I am working with will not allow me to use JQuery. (Please don't argue that with me unless there is absolutely no way to do this in pure JS.)

只需要与Internet Explorer兼容即可。

Needs only to be compatible with Internet Explorer.

我认为这与访问DOM有关,但是我不确定语法的精确度。

I assume this will have to do with accessing the DOM, but I am not sure how exactly the syntax will work out.

推荐答案

由于您未指定可以使用jQuery,请尝试以下操作:

Since you didn't specify that you could use jQuery, try this:

HTML:

<select id="test">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>​

JavaScript:

document.getElementById( test)。options.length

示例

example

这篇关于JavaScript-获取列表选项的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆