繁体   English   中英

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

[英]pop-up window close with “add to cart” button

我正在尝试设置通过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>

好的,所以假设您已经将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 。关于快去午休

暂无
暂无

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

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