【问题标题】:Using anchor link for form submission使用锚链接提交表单
【发布时间】:2011-05-01 11:01:31
【问题描述】:

我可以使用锚标签来提交表单吗?在这段代码中,我使用的是标准的提交按钮,但如何使用锚标签<a href=Cart?removeId=${cartItem.productId>Remove</a>

我试过了,但是在 servlet 中调用了 doGet() 方法。我显然想调用 doPost() 方法。那么我可以使用更好的方法吗?

<c:forEach items="${lstCart}" var="cartItem" varStatus="count">
    <form action="Cart" method=Post>
    <tr height="40px">

        <td>${count.count}</td>
        <td>${cartItem.productName}</td>
        <td>${cartItem.quantity}</td>   
        <td>${cartItem.unitPrice}</td>
        <td>${cartItem.totalPrice}</td>    
        <td>
        <input type="hidden" name="id" value="${cartItem.productId}" />
        <input type=submit value="x"></td>

    </tr>
    </form>
</c:forEach>

【问题讨论】:

    标签: forms jsp servlets anchor form-submit


    【解决方案1】:

    您需要使用 JavaScript 使锚链接发帖。不过,您可以使用 CSS 来设置提交按钮的样式,使其看起来更像一个链接。

    【讨论】:

      【解决方案2】:

      是的,正如@JB 所说,您可以使用 JScript 发布表单。我想到的一个例子是下面的struts,这里你实际上是将产品ID作为变量传递给JS方法(我更改了名称,使其与你的示例相匹配)。

      <a href="#" onclick="removeProduct(${cartItem.productId})">
      Remove
      </a>
      

      然后你就可以有JS方法了

      function removeProduct(productId) {
      document.forms["formname"].elements["productId"].value =
      productId;
      document.forms["formname"].submitTestPost.click(); 
      }
      

      这也假设在页面中定义了以下内容(以便JS可以设置它)和提交属性(标签SubmitTag)。我不确定你的情况,但也许你可以得出一些观点。

      <input type="hidden" name="productId"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-03
        • 2011-05-16
        • 1970-01-01
        • 2015-02-14
        • 2023-03-30
        • 2014-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多