【问题标题】:Differentiate multiple input form inside forEach statement在 forEach 语句中区分多个输入形式
【发布时间】:2020-07-13 22:15:25
【问题描述】:

我有一个字符串数组,长度是可变的。我需要为每个字符串创建两个按钮:buyremove。他们管理相应元素的数量。像这样: Resoult。 我试过这个,有效,但不清楚。

String go = request.getParameter("go");
if ((go != null)){
    String[] info = go.split(",");
    int index = Integer.parseInt(info[1]);
if (info[0].equals("+")) {
    ++quantita[index];
} else {
    --quantita[index];
}}

...

    <c:forEach var="i" begin="0" end="${length-1}" >
        <%
            int i = (int) pageContext.getAttribute("i");
            out.print(products[i] + " (" + quantita[i] +" in cart)");
        %>
        <input type=submit name="go" value="-,${i}"/>
        <input type=submit name="go" value="+,${i}"/><br>
    </c:forEach>

【问题讨论】:

  • 那么您需要其他方法来完成上述操作吗?
  • 是的,我使用了一种策略来解决问题,但代码不清楚。我需要用一种输入形式传递多个参数:id 和 act。我尝试使用隐藏输入表单,但我不知道如何将其放入 forEach 中而不会发生冲突。
  • 尝试使用&lt;table&gt; 所以在你的循环下你会有多个 标签,然后把你的按钮和表单放在那个.ie中:&lt;table&gt;//your loop &lt;tr&gt;&lt;td&gt;//button and form&lt;/td&gt;&lt;/tr&gt;//loop close&lt;/table&gt;

标签: html jsp jstl


【解决方案1】:

使用&lt;button type="submit"&gt; 代替&lt;input type="submit"&gt;。此 HTML 元素允许您通过子元素设置内容,而不是通过 value 属性。这样您就可以简单地使用name 属性来指示操作,并使用value 属性来指示标识符。

<button type=submit name="decrease" value="${i}">-</button>
<button type=submit name="increase" value="${i}">+</button>
String decrease = request.getParameter("decrease");
String increase = request.getParameter("increase");

if (decrease != null) {
    --quantity[Integer.parseInt(decrease)];
}
else if (increase != null) {
    ++quantity[Integer.parseInt(increase)];
}

这样更清楚吗?

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 2023-03-30
    • 2019-11-23
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多