我在 javascript 中有一个小表单,其中包含两个具有相同类名的输入值。我知道您可以通过给他们 ID 来定位他们,但是有没有办法使用数组值来定位他们?
<td>
<input class="inputtextb" type="text" name="adults" maxlength="3"> Adults
<input class="inputtextb" type="text" name="children" maxlength="3"> Children above 5 years
最佳答案
您可以使用 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/