简体   繁体   English

Javascript / jQuery重定向到页面

[英]Javascript/jQuery redirect to page

I am using simplecart js. 我正在使用simplecart js。 It's a really simple shop with one product. 这是一家非常简单的商店,只有一种产品。 I would like to automatically redirect to the cart page when someone clicks the add to cart button. 当有人单击添加到购物车按钮时,我想自动重定向到购物车页面。

The code for adding a product: 添加产品的代码:

<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>

The listener in simpleCart.js: 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);
                    }
                }
            ]);
        });

From what I have read it is bad practice to use href="javascript:;" 据我了解,使用href =“ javascript :;”是一种不好的做法 is it a good idea to change it to a click function that will add the item to the cart then go to the cart page or just add the redirect? 将其更改为将功能添加到购物车,然后转到购物车页面或仅添加重定向的点击功能是个好主意吗? How do I go about this? 我该怎么办? Thanks 谢谢

I'm not sure how the simplecart APi works, but you can try something like: 我不确定simplecart APi的工作方式,但是您可以尝试以下方法:

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

If the cart saves to a server cookie, you might need to put this in a callback. 如果购物车保存到服务器Cookie,则可能需要将其放入回调中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM