简体   繁体   English

如何在提交表单之前更改表单元素的ID?

[英]how to change id of form elements before form gets submitted?

I have a table with many rows and each row has select lists. 我的表有很多行,每行都有选择列表。 Something like this: 像这样:

| USER    | MANAGER            | DEPARTMENT   |

| Rob     | [John Smith |V]    | [Sales |V]   | 

| Sue     | [Bob Jones |V]     | [Support |V] |

The user is free to add new rows, there are many rows and the contents of the lists are complex. 用户可以自由添加新行,行很多,列表的内容很复杂。

I simplified things considerably by giving every manager selector id 'manager', and every department selector id 'department'. 我给每个经理选择器ID为“ manager”,每个部门选择器ID为“ department”,从而大大简化了事情。

When the form is submitted, I wrote some javascript to cycle through each row, locate each row, and change the ids 'manager_[rownumber]' and 'department_[rownumber]'. 提交表单后,我编写了一些JavaScript来遍历每一行,找到每一行,然后更改ID“ manager_ [rownumber]”和“ department_ [rownumber]”。

And then I submit the form. 然后我提交表格。 My javascript code is below. 我的JavaScript代码如下。

Based on debugging with alert popups, the ids are getting changed the way I want, but the servlet only ever receives one 'manager' parameter and one 'department' row number. 基于带有警报弹出窗口的调试,id会按照我想要的方式进行更改,但是servlet仅收到一个“ manager”参数和一个“ department”行号。

Why aren't all the inputs ('manager_1', 'manager_2', and so on) getting submitted? 为什么不提交所有输入(“ manager_1”,“ manager_2”等)? Is there something I have to do after changing the ids to make sure the changes take effect before the submit of the form? 更改ID之后,我需要做些什么以确保更改在提交表单之前生效吗?

Thanks! 谢谢!

Rob

Here's my code: 这是我的代码:

function submitForm() {
  correctInputIDs();
  document.myform.submit();
}

function correctInputIDs() {
  var rows = document.getElementsByTagName("tr");
  for (var i=0; i<rows.length; i++) {
    var row = rows[i];
    selectElements = rows.getElementsByTagName("select");
    for (var j=0; j<selectElements.length; j++) {
      var selectElement = selectElements[j];
      if (selectElement.id == "manager") {
        selectElement.id = "manager_"+i;
      }
      if (selectedElement.id == "department") {
        selectElement.id = "department_"+i;
      }
    }
  }
}

Because IDs don't matter with form submission, name s do. 因为ID与表单提交无关紧要,所以name s无关紧要。

Happy Reading 快乐阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM