次のコード:
<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>