2011-05-13 99 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