【问题标题】:J-Query Beginner - Issue with JQuery Paypal Shopping CartJ-Query 初学者 - JQuery Paypal 购物车问题
【发布时间】:2014-01-22 03:09:01
【问题描述】:

我正在使用下面的脚本在我的 html 页面上添加一个 Paypal 购物车

<!-- CSS files -->
<link rel="stylesheet" type="text/css" href="js/jPayPalCart.css" />    

<!-- Script files -->
<script src="js/jPayPalCart.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function(){
        // Create a basic cart
        $("#myCart").PayPalCart({ business: 'yourselleremail@yourdomain.com',
            notifyURL: 'http://www.yournotifyURL.com/notify.php',
            virtual: false,             //set to true where you are selling virtual items such as downloads
            quantityupdate: true,       //set to false if you want to disable quantity updates in the cart 
            currency: 'GBP',            //set to your trading currency - see PayPal for valid options
            currencysign: '£',          //set the currency symbol
            minicartid: 'minicart',     //element to show the number of items and net value
            persitdays: 0               //set to -1 for cookie-less cart for single page of products, 
                                        // 0 (default) persits for the session, 
                                        // x (number of days) the basket will persits between visits
            });
    });
</script> 

我面临的问题是:我无法弄清楚如何将以下代码实现到我的 html 项目按钮以将项目添加到购物车

$("myCart").PayPalCart('add', code, description, quantity, value, vat);

我的 html 页面上的项目显示为

<div class="grid_3">        
    <div class="pricing-table">
        <div class="top">
        <img src="images/p6.jpg" alt=""><br><span class="permonth">"AIMLESS" Gloves</span>
        </div>
        <div class="title">
        $11.95
        </div>
            <ul class="specifications">
                <li>"Aimless" Logo Embroidered Gloves are touch-screen-friendly with conductive thread on the tip of the thumb and fingers.</li>
                <li></li>
                <li></li>
            </ul>
        <p><a class="button" href="#">Buy Now</a></p>
    </div>
</div>

非常感谢您在这个主题上的专业知识这是我在这个问题上的第二天。如果有帮助,我按照此网页上的说明进行操作http://www.ultimateweb.co.uk/jPayPalCart/

脚本不是由 PayPal 编写的,这是一个定制的 JQ 购物车,旨在将 Paypal 用作结帐支付终端。购物车完美运行,但我不知道如何让我的“立即购买”html 按钮将商品添加到购物车。我从上面下载脚本的网站声明使用这一行:

$("myCart").PayPalCart('add', code, description, quantity, value, vat);  

将商品添加到我的购物车

感谢您的宝贵时间,非常感谢您的帮助

当我运行控制台以使用谷歌浏览器检查页面上的错误时,这是​​我得到的错误

Uncaught ReferenceError: jQuery is not defined custom.js:1
(anonymous function) custom.js:1
Uncaught ReferenceError: jQuery is not defined jPayPalCart.js:223
(anonymous function) jPayPalCart.js:223
Uncaught ReferenceError: $ is not defined index.php:31
(anonymous function) index.php:31
Uncaught ReferenceError: $ is not defined index.php:47
(anonymous function) index.php:47

【问题讨论】:

  • Paypal 的文档在哪里?
  • 我只对让 JQ 命令将商品添加到购物车感兴趣,其他一切正常

标签: javascript jquery html


【解决方案1】:

您需要为“立即购买”链接注册一个点击处理程序。您还可以使用data- 属性来保存商品的数据,然后在调用 PayPalCart 的“添加”函数时使用这些属性。

添加到您的“立即购买”链接:

<a class="button buyNow" href="#"
    data-code="theCode"
    data-description="theDescription"
    data-quantity="1"
    data-value="theValue"
    data-vat="theVat">Buy Now</a>

然后添加此代码为“立即购买”链接注册一个点击处理程序:

<script type="text/javascript">
    $(document).ready(function(){
        $('.buyNow').click(function(event) {
            event.preventDefault();
            var $link = $(this);
            $('#myCart').PayPalCart('add',
                $link.attr('data-code'),
                $link.attr('data-description'),
                $link.attr('data-quantity'),
                $link.attr('data-value'),
                $link.attr('data-vat'));
        });
    });
</script>

通过这种方式,您可以为不同的商品提供多个“立即购买”链接,并且此点击处理程序适用于所有商品。如果需要,您可以将此代码放在您已有的 &lt;script&gt; 元素中。

【讨论】:

  • 非常感谢您,让我试试,我会回复您,让您知道会发生什么
  • 点击什么也没发生 :(
  • @Loading - 您是否像我在回答中显示的那样将“buyNow”类添加到&lt;a&gt; 元素?这就是我选择用于注册点击处理程序的链接的方式。除了注册点击处理程序之外,您还应该从这个答案中删除三件事:(1)使用类来识别元素,(2)在点击处理程序中使用 this 以及 data- 属性所以点击处理程序适用于多个元素,以及 (3) 在点击处理程序中为 &lt;a&gt; 元素调用 event.preventDefault(),这样浏览器就不会导航到 href 属性的值。
  • 我非常肯定你是对的,我对 jQuery 完全陌生,所以任何短语都需要我大量时间来研究。给我一些时间根据你的评论来解决这个问题,我会用我得到的回复你......再次感谢你在这方面陪伴我
  • 我很遗憾地告诉你我还是做不到 :( 这是我的索引文件的链接ge.tt/2ijA8wF1/v/0 按钮在第 635 行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多