gpt4 book ai didi

javascript - 在 javascript 中使用数组值定位表单元素

转载 作者:行者123 更新时间:2023-11-30 13:09:37 25 4
gpt4 key购买 nike

我在 javascript 中有一个小表单,其中包含两个具有相同类名的输入值。我知道您可以通过给他们 ID 来定位他们,但是有没有办法使用数组值来定位他们?

 <td>
<input class="inputtextb" type="text" name="adults" maxlength="3">&nbsp;Adults&nbsp;
<input class="inputtextb" type="text" name="children" maxlength="3">&nbsp;Children above 5 years&nbsp;

最佳答案

您可以使用 document.getElementsByClassName() 为此:

var inputs = document.getElementsByClassName('inputtextb');

inputs[0] // the first input
inputs[1] // the second input

如果这不起作用 - can i use getElementsByClassName - 你可以遍历所有 <input>元素使用 document.getElementsByName()并过滤掉你想要的;或者使用 jQuery ;-)

关于javascript - 在 javascript 中使用数组值定位表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14380942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com