2011-05-13 138 views
-1

我是新来的ajax,就像它可以异步做事,而不必重新加载页面。如果我想更新我的购物车总量而不必实际点击“更新”按钮,可以使用XMLHttpRequest对象完成。谢谢您的帮助。异步更新

<% 
ShoppingCart cart = (ShoppingCart)session.getAttribute("cart"); 
if(cart==null){ 
response.sendRedirect("manageCart.jsp"); 
}else{ 
//We have the cart, let's update it 
int itemCount = cart.getItemCount(); 
int quant = 0; 
for(int i=itemCount-1; i>=0; i--){ 
    try{ 
     //User may have erased quantity or entered non-numeric value 
     quant = new Integer(request.getParameter("item" + i)).intValue(); 
    }catch(NumberFormatException nfe){ 
     quant = 1; 
    } 
    //Get the next Item 
    Item item = cart.getItem(i); 
    //Set its quantity 
    item.setQuantity(quant); 
    if(request.getParameter("remove" + i)!=null){ 
     //Remove checkbox checked 
     cart.removeItem(i); 
    } 

} 
//Put the cart back in the session 
session.setAttribute("cart",cart); 
//go look at it! 
response.sendRedirect("manageCart.jsp"); 
} 
%> 
+0

这个问题太宽泛了。通过教程来掌握基本概念。把你的项目放在一边,制定一些基本的教程/例子。这是一个很好的起点:http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet/4113258#4113258 – BalusC 2011-05-13 19:59:42

回答

0

阿贾克斯只处理部分“更新令”发出后。如果你想避免必须点击更新按钮,你需要以不同的方式触发Ajax请求,也许使用某个sor的set-interval函数?

+0

所以无论哪种方式,我必须用ajax按钮,但它只会获取信息没有点击等待加载? – Hedgehodge 2011-05-13 19:55:08