如何使用jQuery计算选项标签的数量?

Mav*_*ick 18 html jquery html-select count

我有一个问题,当我有多个具有相同类和ID的选择框时,如何计算选项标签的数量?

假设我有三个选择框.我想要选择框的大小,以便我可以动态添加具有相同选项的新选择框:

<select id="selectid" class="selectclass">
    <option>1</option>
    <option>2</option>
</select>

<select id="selectid" class="selectclass">
    <option>1</option>
   <option>2</option>
</select>

<select id="selectid" class="selectclass">
    <option>1</option>
    <option>2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

man*_*nji 63

用jquery:

对于给定selectid:

$('select#selectid option').length
Run Code Online (Sandbox Code Playgroud)

对于所有selects具有给定类的人:

$('select.selectclass option').length
Run Code Online (Sandbox Code Playgroud)

对于selects页面中的所有人:

$('select option').length
Run Code Online (Sandbox Code Playgroud)

但你应该给html页面中的每个元素赋予不同的ID