gpt4 book ai didi

javascript - 更改所有具有 id= 值的元素

转载 作者:行者123 更新时间:2023-11-28 14:55:34 25 4
gpt4 key购买 nike

我有一个由 php for 循环生成的输入按钮。

这是它的输出:

<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Submit">

如何更改 id 或名称为“submit”的所有输入的值?

我尝试了getElementById,但仅更改了输入:

<script>
var submit_button = document.getElementById('submit');
submit_button.value = 'Next';
</script>

我尝试了getElementsByName,但根本不起作用:

<script>
var submit_button = document.getElementsByName('submit');
submit_button.value = 'Next';
</script>

如何更改所有输入的值?

我想要常规的 JavaScript。如果常规 javascript 无法实现,jquery 也可以。

最佳答案

ID 必须是唯一的,因此请更改它们。

您可以使用document.getElementsByName 。因为这会返回 NodeList您可以使用 Array.forEach以便更改 value 属性。

document.getElementsByName('submit').forEach(function(ele, idx) {
ele.value = 'Next';
})
<input type="submit" name="submit" id="submit1" value="Submit">
<input type="submit" name="submit" id="submit2" value="Submit">
<input type="submit" name="submit" id="submit3" value="Submit">
<input type="submit" name="submit" id="submit4" value="Submit">
<input type="submit" name="submit" id="submit5" value="Submit">
<input type="submit" name="submit" id="submit6" value="Submit">
<input type="submit" name="submit" id="submit7" value="Submit">
<input type="submit" name="submit" id="submit8" value="Submit">

关于javascript - 更改所有具有 id= 值的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022336/

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