设置数组输入php的值

时间:2018-07-18 06:57:33

标签: javascript html getelementbyid

我有这样的代码:

资料1

<input type="text" id="stuff" name="stuff[]" />

<input type="text" id="more_stuff" name="more_stuff[]" />

东西2

<input type="text" id="stuff" name="stuff[]" />

<input type="text" id="more_stuff" name="more_stuff[]" />

我需要设置输入,我知道要设置的值是:

$string="arifin";

document.getElementById("stuff").value=$string;

但是该代码仅更改第一行(第一个输入内容)。如何设置第二个东西输入的值?

1 个答案:

答案 0 :(得分:0)

您可以选择所有元素作为document.querySelectorAll('[id ^ = more_stuff]')

var ele = document.querySelectorAll('[id^=more_stuff]');
$string="arifin";
for(var i in ele){
   ele[i].value=$string;
}

如果要用于课堂,只需将其更改为

document.querySelectorAll('.classname');
相关问题