在表单表中设置隐藏的输入值

时间:2018-10-02 12:32:03

标签: javascript java html jsp servlets

我有一张这样的桌子:

<%
        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>
            <% 
            }
        }
        %>

现在,我要将提交按钮的值传递给隐藏的输入值,我做了以下javascript函数:

<script type="text/javascript">
    function setQuota(quota) {
        document.forms["action"].elements["quota"].value = quota;


    }
</script>

但是它仅适用于表的第一行。 我该如何解决?问题是在while循环中还是在其他地方?

2 个答案:

答案 0 :(得分:1)

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;
  }
}

答案 1 :(得分:1)

向输入元素中添加一个dummy_css_class;

<script type="text/javascript">
function setQuota(quota) {
    document.getElementsByClassName("dummy_css_class ").value = $YourValue
}