2

以下代码:

<button type="button" id="button" onclick="<%cart.removeItem(0);%>">Click me</button>

假设在单击按钮时执行。然而,

 "<%cart.removeItem(0);%>"

当页面刷新而不单击按钮时正在执行。为什么会这样?

干杯。

这是完整的来源。

   <html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
   </head>
   <body>
    <jsp:useBean id="cart" scope="session" class="myBeans.cart" />
    <%
    cart.addItem("aji", "1000", "1");
    cart.addItem("ewer", "200", "1");
    cart.addItem("dfwerweji", "10", "1");
    cart.addItem("ldsjioi", "1320", "1");

    String[] prodNames = cart.getProdNames();
    double[] prices = cart.getProdPrices();
    int[] qtys = cart.getProdQtys();
    double total = 0;

    for(int i=0; i<prodNames.length; i++){
    total += prices[i]*qtys[i];
    out.println(prodNames[i]);
    out.println(" " + prices[i] + " ");
    out.println("<input type=text name=newQty value=" + qtys[i] + ">");
    out.println(" " + prices[i] * qtys[i]);
    }
    %>
    <br/>
    <button type="button" id="button" onclick="<%cart.removeItem(0);%>">Click me</button>
    </body>
    </html>
4

1 回答 1

2

我认为您在这里混淆了您的语言。我怀疑“购物车”是一个 Java 对象,您只能在客户端修改 JavaScript 对象。你必须有这样的东西才能使它工作:

<script> 
  doRemoveFirst = function() { new Ajax.Request('removeFirst.page'); }; 
</script>
<button type="button" id="button" onclick="doRemoveFirst();">Click me</button>

然后在服务器上有一个名为“removeFirst”的页面,该页面将从 Java 对象中删除该对象(可能保留在会话中?),您可以相应地更新您的页面。

编辑:这是一个帮助的图像。对角线左边的一切都是客户端,而右边的一切都是服务器端。

这个图片

编辑 2:为用户删除和修复页面

我会说类似(假设 jQuery)这可能对你有用。

$(".item-row").first().remove(); 
$(".item-row").each(function(idx, el) {
  var elem = $(el).children().find('.index-cell');
  elem.text(+elem.text() - 1);
});
于 2011-08-02T16:35:31.403 回答