简体   繁体   English

在循环下从具有相同name属性的html表单获取值

[英]Getting Value from html form with the same name attribute under Loop

I have a form which displays multiple rows from database with 4 columns. 我有一个表格,其中显示了数据库中具有4列的多行。 From these record I need to write a new value in 4th column and update database record. 从这些记录中,我需要在第4列中写入一个新值并更新数据库记录。 But whenever I try, only First Row value can be updated/read. 但是,只要我尝试,就只能更新/读取“第一行”值。 But not the other rows!! 但是没有其他行! This can be due to the same "name=redirection" as it is given to each from "for loop". 这可能是由于与“ for循环”中的每个名称都具有相同的“名称=重定向”。 So, how can I get the values from other rows too?? 因此,我如何也可以从其他行中获取值呢?

for (int i=0; i<domains.size(); i++) {
domainprops = (String[]) domains.get(i);   
%>

<table cellspacing="0" cellpadding="10" border="0" class="tableview" width="100%">
<td width="150"><input type="text"  id="domains" name="domains" value="<%=domainprops[0]%>"></td>
<td width="160"><input type="text"  name="defaulturl" value="<%=domainprops[1]%>" size="30"></td>
<td width="160"><input type="text"  name="redirecturl" value="<%=domainprops[2]%>" size="30"></td>

<td width="160"> <input type="text" id="redirection" name="redirection"></td>

<td align="right"><a href="javascript:win2('recordUpdate.jsp?domains=<%=domainprops[0]%>
')">[Update]</a></td>

    </tr>
 </table>

<% } %>

Javascript Code : JavaScript代码:

function win2(urlPath) {
    var winl = (screen.width-200)/2;
    var wint = (screen.height-100)/2;
    var settings = 'height=100,width=200,directories=no,resizable=no,status=no,scrollbars=no,menubar=no,location=no,top=' + wint + ',left=' + winl;

    var changeurls=document.getElementById("redirection").value;
    urlPath+='&rdirect='+changeurls
    editWin.focus();
}

An ID in the DOM is supposed to be unique. DOM中的ID应该是唯一的。 If any element in the DOM has an ID, it should not be shared by any other element. 如果DOM中的任何元素具有ID,则不应由任何其他元素共享。

What I would suggest doing is appending your loop counter on to the end of the ID. 我建议做的是将循环计数器附加到ID的末尾。 This will ensure that every element you create in the DOM has its own unique ID. 这将确保您在DOM中创建的每个元素都有其自己的唯一ID。

for (int i=0; i<domains.size(); i++) {
domainprops = (String[]) domains.get(i);   

...
<input type="text" id="domains_<%= i %>" name="domains" value="<%=domainprops[0]%>">
...
<input type="text" id="redirection_<%= i %>" name="redirection"></td>



</tr>
</table>

}

Next, pass the loop counter to the win2 function call: 接下来,将循环计数器传递给win2函数调用:

<td align="right"><a href="javascript:win2('recordUpdate.jsp?domains=<%=domainprops[0]%>
', <%= i %>)">[Update]</a></td>

Finally, adjust the function itself... 最后,调整功能本身...

function win2(urlPath, loopID) {
...
    var changeurls=document.getElementById("redirection_" + loopID).value;
    urlPath+='&rdirect='+changeurls
...
}

EDIT: Please read the answer referring to having multiple elements with the same ID. 编辑:请阅读答案,指的是具有相同ID的多个元素。 You should not be using multiple of the same ID. 您不应该使用多个相同的ID。

You could use Javascript to iterate over redirection form elements. 您可以使用Javascript遍历重定向表单元素。

function loopThroughRedirection(form) {
    var result = "";

    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].name == 'redirection') {
            // Do something to retrieve the value of redirection
            result += form.elements[i].value
        }
    }

    return result;
}

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

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