弹出窗口关闭“添加到购物车”按钮

时间:2013-12-04 13:01:54

标签: javascript php

我正在尝试设置通过JavaScript调用的购物车表单功能(添加到购物车按钮)。我在弹出窗口中有这个,并希望“添加到购物车”按钮关闭弹出窗口,并在主窗口中加载信息。有谁知道我需要修改哪些脚本?

以下是弹出窗口中我当前的代码,用于生成添加到购物车:

<form class="cart" action="index-shop.php" method="post">
    <input type="hidden" name="my-item-id" value="book 1" />
    <input type="hidden" name="my-item-name" value="book 1" />
    <input type="hidden" name="my-item-price" value="35.00" />
    <input type="hidden" name="my-item-url" value="http://www.amazoni.com" />
    Qty: <input type="text" name="my-item-qty" value="1" size="3" />
    <input class="button" type="submit" name="my-add-button" value="add to cart" />
</form>

1 个答案:

答案 0 :(得分:0)

好的,假设你已经在你的网页中包含了jquery文件之前的某个地方,这就是我要解决你所遇到的问题的方法;

<form class="cart">
   <input type="hidden" name="my-item-id" value="book 1" />
   <input type="hidden" name="my-item-name" value="book 1" />
   <input type="hidden" name="my-item-price" value="35.00" />
   <input type="hidden" name="my-item-url" value="http://www.amazoni.com" />
   Qty: <input type="text" name="my-item-qty" value="1" size="3" />
   <input class="button" type="submit" id="my-add-button" name="my-add-button" value="add to cart" />
</form>
<script type="type/JavaScript">
   $(document).ready(function(){
      $(".cart").submit(function(e){
         e.preventDefault(); //prevent the form from submitting
      });

      //send the form to the server mimicking the post behaviour, without submitting the form
      $("#my-add-button").click(function(){
         $.post("index-shop.php", $(".cart").serialize(), function(data){
            if(data.success){ // this is assuming you created a variable indicating whether server processing succeeded
               /*
               * here we insert the post server processing code
               * and then we close the dialog
               */
            }else{
               alert(data.error); //if there's an error we display the error message and keep the dialog window open allowing the user to continue
            }
         });

         return false;
      });
   });
</script>

如果您遇到问题或需要更多指导,请回复我。 顺便说一下,我是南非人;我们的时间现在是13:30。 。我很快就会吃午饭了