循环使用不同名称的表单字段

问题描述:

所以我有一个带有一堆文本字段的表单。有些字段是相关的,我需要遍历它们并在值字段中加上数字。

So I have a form with a bunch of Text fields. Some of the fields are related and I need to loop through them and add up the numbers in the value field.

这些字段的名称都不同,所以我可以区分它们所以我不确定如何浏览它们。

The fields are all named differently so I can tell them apart so I'm not sure how to look through them.

我可以将它们全部设置为相同的ID或相同的东西然后根据共享属性进行循环吗?我宁愿不必手动完成它们,但如果必须的话可以这样做。

Can I set them all to the same ID or same something to then do the loop based on the shared property? I'd rather not have to manually go through them all but can do that if I have to.

我想也许如果所有字段都是表单数组的一部分我可以找出我想要的字段在哪里开始和停止并以这种方式循环它们?嗯

I guess maybe if all the fields are part of a form array I can figure out where the fields I want start and stop and loop them that way? hmmm

给他们所有的班级名称。例如。 class =inputs
将它们添加到数组中,然后循环使用 for 循环。

Give them all a class name. Eg. class="inputs" . Add them to an array, then loop through with a for loop.

var inputs = document.getElementsByClassName("inputs");
for(i =0; i < inputs.length; i++) 
{
  // whatever you want to do now.
}