2012-09-03 55 views
0

我正在使用simplecart js。這是一個非常簡單的商店,只有一種產品。當有人點擊添加到購物車按鈕時,我想自動重定向到購物車頁面。Javascript/jQuery重定向到頁面

用於添加產品代碼:

<div class="simpleCart_shelfItem"> 
<h2 class="item_name"> Awesome T-shirt </h2> 
<p> <input type="text" value="1" class="item_Quantity"><br> 
<span class="item_price">$35.99</span><br> 
<a class="item_add" href="javascript:;"> Add to Cart </a></p> 
</div> 

聽者simpleCart.js:

/* here is our shelfItem add to cart button listener */ 
       , { selector: 'shelfItem .item_add' 
        , event: 'click' 
        , callback: function() { 
         var $button = simpleCart.$(this), 
          fields = {}; 

         $button.closest("." + namespace + "_shelfItem").descendants().each(function (x,item) { 
          var $item = simpleCart.$(item); 

          // check to see if the class matches the item_[fieldname] pattern 
          if ($item.attr("class") && 
           $item.attr("class").match(/item_.+/) && 
           !$item.attr('class').match(/item_add/)) { 

           // find the class name 
           simpleCart.each($item.attr('class').split(' '), function (klass) { 
            var attr, 
             val, 
             type; 

            // get the value or text depending on the tagName 
            if (klass.match(/item_.+/)) { 
             attr = klass.split("_")[1]; 
             val = ""; 
             switch($item.tag().toLowerCase()) { 
              case "input": 
              case "textarea": 
              case "select": 
               type = $item.attr("type"); 
               if (!type || ((type.toLowerCase() === "checkbox" || type.toLowerCase() === "radio") && $item.attr("checked")) || type.toLowerCase() === "text") { 
                val = $item.val(); 
               }    
               break; 
              case "img": 
               val = $item.attr('src'); 
               break; 
              default: 
               val = $item.text(); 
               break; 
             } 

             if (val !== null && val !== "") { 
              fields[attr.toLowerCase()] = fields[attr.toLowerCase()] ? fields[attr.toLowerCase()] + ", " + val : val; 
             } 
            } 
           }); 
          } 
         }); 

         // add the item 
         simpleCart.add(fields); 
        } 
       } 
      ]); 
     }); 

從我讀它是不好的做法,使用HREF = 「JavaScript的:;」將它改爲點擊功能是否是一個好主意,它會將商品添加到購物車,然後轉到購物車頁面或僅添加重定向?我如何去做這件事?由於

回答

1

我不知道怎麼simplecart API的工作,但你可以嘗試這樣的:

// add the item 
simpleCart.add(fields); 
window.location='/cart/'; // change to your cart route 

如果車保存到服務器cookie,你可能需要把這個回調。

+0

簡單而有效!謝謝! – Anthony