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>

次に、そのオブジェクトを Java オブジェクトから削除する「removeFirst」と呼ばれるページをサーバー上に作成し (セッションに保持される可能性がありますか?)、それに応じてページを更新できます。

編集:ここに役立つ画像があります。対角線の左側はすべてクライアント側で、右側はすべてサーバー側です。

この画像

EDIT 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 に答える