我有一张这样做的桌子:

<%
        EventoModel model = new EventoModel();
        Collection<EventoBean> eventi = new ArrayList<EventoBean>();

        eventi = model.doRetrieveAll();

        if(eventi != null & eventi.size() > 0) {
            Iterator<?> it = eventi.iterator();
            while (it.hasNext()) {
                EventoBean bean = (EventoBean)it.next();
                %>
            <form method="get" action="./CartServlet" name="action">
            <table id="customers">
            <tbody>
                <tr>

                    <td><%= bean.getEvento()  %></td>


                    <td><%= bean.getPalinsesto() %></td>
                    <td><%= bean.getCampionato() %></td>
                    <td><%= bean.getData()%></td>
                    <td><%= bean.getSquadraCasa() %></td>
                    <td><%= bean.getSquadraTrasferta() %></td>
                    <% String quota1 = bean.getQuota1(); %>
                    <td>
                        <input type="hidden" value="1" name="action">
                        <input type="hidden" value=<%= bean.getPalinsesto() %> name="palinsesto">
                        <input type="hidden" value=<%= bean.getEvento() %> name="evento">
                        <input type="hidden" name="quota" id="hiddenQuota" value="">

                        <input type="submit" value=<%= quota1 %> onclick="setQuota(<%= quota1 %>);"></td>
                    <% String quotaX = bean.getQuotaX(); %>
                    <td><input type="submit" value=<%= quotaX %> onclick="setQuota(<%= quotaX %>);"></td>
                    <% String quota2 = bean.getQuota2(); %>
                    <td><input type="submit" value=<%= quota2 %> onclick="setQuota(<%= quota2 %>');"></td>

                </tr>
            </tbody>
            </table>
            </form>
            <%
            }
        }
        %>

现在我想将submit按钮的值传递给隐藏的输入值,我制作了这个javascript函数:
<script type="text/javascript">
    function setQuota(quota) {
        document.forms["action"].elements["quota"].value = quota;


    }
</script>

但它只对表的第一行起作用。
我怎样才能修好它?问题是在while循环中还是在其他地方?

最佳答案

function setQuota(quota) {
var myFormElements = document.forms["action"].elements
for (i = 0; i < myFormElements.length; i++) {
  if (myFormElements[i].nodeName === "INPUT" && myFormElements[i].name === "quota") {
    myFormElements[i].value = quota;
  }
}

08-26 15:36