答案 0 :(得分:0)
我刚刚在名为selectAll
的提交上添加了一项功能,并使用select
将multiple
设为ID cart
。如果你使用jquery代码可以进一步减少。
<form method="POST" action="servlet" onsubmit="selectAll">
<div id="products-container">
//I use this list to pick the things I want
<select id="product-list" size="26" onclick="addToList(this)">
<%
List<Product> product = ProductDao.loadProducts();
for(int i = 0; i < product.size(); i++){
%>
<%
out.write(
"<option value=\"" + product.get(i).getProductId() + "\">"
+ product.get(i).getProductName() + "</option>");
%>
<%
}
%>
</select>
</div>
<div id="cart-container">
//I use this list to save all the things I already picked,
//if I double click an item on the list, it removes said item
<select id="cart" size="10" name="cart" onDblClick="removeFromList()" multiple>
</select>
</div>
<input type="submit" value="Buy">
</form>
<强>的javascript:强>
function selectAll(){
var cartOptions = document.getElementById("cart").getElementsByTagName("option");
for(var i=0;i<cart.length;i++){
cartOptions[i].setAttribute("selected","true");
}
}