English 中文(简体)
通过在文本框中输入多产品 ID,在购物车中添加多个驱动器
原标题:adding multiple produt to shopping cart by entering multiple product ID in a textbox

I am developing an option in Magento where the customer can enter multiple product ID s in a text box and add them to Shopping Cart. I have been able to do it for only one product ID where it is entered in a input box by using the following jquery,

$(document).ready(function(){
        $("#submit").click(function(){
            var id = $("#nam").val();
            $.post("....../checkout/cart/add", { product:id },
            function(data) {
                alert("The Product is added to your shopping cart.");
                window.location.reload(true);
            });
        });
    });

var id 获取输入框的值。

In an input box I am taking one product ID and so it s no problem in fetching the value and passing it to the add product page by using jquery.post(). But if I enter several product ID s separated by comma in a text box how will I fetch each productID and send it to add product page for updating ? Is there any better way to do it other than using jquery.post() method ?

最佳回答

您可以用一个分隔符分割你的标识, 并在所生成的向购物卡添加项目的数组中循环。 例如 :

$(document).ready(function(){
        $("#submit").click(function(){
            var ids = $("#nam").val().split(   ); // SPACE seperated Ids
            for(i = 0; i < ids.length; i++){
              var id = ids[i];
              $.post("....../checkout/cart/add", { product:id },
              function(data) {
                //alert("The Product is added to your shopping cart.");
                //window.location.reload(true);
              });
            }
        });
    });

在这样做之前, 您必须先净化您的文本框值 。

问题回答
 $(document).ready(function () {
                $("#submit").click(function () {
                    var productIds = [];

                    $.each($("#nam").val().split( , ), function (i, value) {
                        if (value.length > 0) {
                            productIds.push(value);
                        }
                    });

                    $.post("....../checkout/cart/add", { productIds: productIds },
                                                          function (data) {
                                                              alert( The  + (productIds.length == 1 ?  Product  :  Products ) +   is added to your shopping cart. );
                                                              window.location.reload(true);
                                                          });
                });
            });

            public ActionResult add(int[] productIds)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

您需要返回一系列产品代号以张贴动作,然后才能实现。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签