使用Javascript按名称获取包含字符串一部分的元素

时间:2018-12-28 17:59:22

标签: javascript html dom

我试图使用Javascript选择名称值包含字符串“ visitors”的所有元素。在这种情况下,我无法添加课程。

<select name="visitors10">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>
<select name="visitors11">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>
<select name="visitors12">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>

2 个答案:

答案 0 :(得分:0)

尝试一下

document.querySelectorAll("select[name^=visitors]")

答案 1 :(得分:0)

您可以使用querySelectorAll

这样操作

<select name="visitors10">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>
<select name="visitors11">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>
<select name="visitors12">
   <option class="disableDuration4" value="5">5</option>
   <option class="disableDuration5" value="6">6</option>
   <option class="disableDuration6" value="7">7</option>
</select>
<select name="xyz"></select>
<select name="visitorsxyz"></select>
bc