简体   繁体   English

JavaScript 将商品添加到购物车已损坏

[英]JavaScript to add an item to the cart is broken

I've got a button, which is part of the provided code in my Magento theme, and according to the date/time stamp, I haven't inadvertantly edited it.我有一个按钮,它是我的 Magento 主题中提供的代码的一部分,根据日期/时间戳,我没有无意中编辑它。 I'm sure that it was working at some point, but a glance back into my source control over the last week, and I can't seem to track down where things went wrong.我确信它在某个时候可以正常工作,但是回顾一下上周我的源代码控制,我似乎无法找出哪里出了问题。

Here is the button HTML:这是按钮 HTML:

<button type="button" title="Add to Cart" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Add to Cart</span></span></button>

... but when I click on it nothing happens. ...但是当我点击它时,什么也没有发生。 Seems pretty straight forward, except I can't see if/where there is a typo, etc. So, I check Firebug and I see the following error:看起来很简单,除了我看不到是否/哪里有错别字等。所以,我检查了 Firebug,我看到了以下错误:

在此处输入图像描述

However, when I go to "View page source", the script is indeed in the page:但是,当我 go 到“查看页面源代码”时,脚本确实在页面中:

<script type="text/javascript">
//<![CDATA[
    var productAddToCartForm = new VarienForm('product_addtocart_form');
    productAddToCartForm.submit = function(button, url) {
        if (this.validator.validate()) {
            var form = this.form;
            var oldUrl = form.action;

            if (url) {
               form.action = url;
            }
            var e = null;
            try {
                this.form.submit();
            } catch (e) {
            }
            this.form.action = oldUrl;
            if (e) {
                throw e;
            }

            if (button && button != 'undefined') {
                button.disabled = true;
            }
        }
    }.bind(productAddToCartForm);

    productAddToCartForm.submitLight = function(button, url){
        if(this.validator) {
            var nv = Validation.methods;
            delete Validation.methods['required-entry'];
            delete Validation.methods['validate-one-required'];
            delete Validation.methods['validate-one-required-by-name'];
            if (this.validator.validate()) {
                if (url) {
                    this.form.action = url;
                }
                this.form.submit();
            }
            Object.extend(Validation.methods, nv);
        }
    }.bind(productAddToCartForm);
//]]>
</script>

The $ variable is in conflict with other JavaScript libraries being used. $变量与正在使用的其他 JavaScript 库冲突。 Removing the inclusion of the jQuery library should bring back the other functionality, in order to prove that is the problem.删除包含 jQuery 库应该会恢复其他功能,以证明这是问题所在。

In order to fix it, either rewrite the jPlayer code (replacing $ with jQuery ), or try using the jQuery.noConflict() function.为了修复它,要么重写 jPlayer 代码(将$替换为jQuery ),要么尝试使用jQuery.noConflict() function。

Ie, this: $(document).ready(function(){ ...即: $(document).ready(function(){ ...

... becomes that: jQuery(document).ready(function(){ ... ... 变成: jQuery(document).ready(function(){ ...

More details can be found in the jQuery.noConflict() documentation.更多详细信息可以在jQuery.noConflict()文档中找到。

In this particular example in my question above, I solved it by using noConflict() in the following way:在我上面的问题的这个特定示例中,我通过以下方式使用noConflict()解决了它:

$.noConflict();
jQuery(document).ready(function(){
  jQuery("#jquery_jplayer").jPlayer({
    ready: function () {      
      jQuery(this).jPlayer("setMedia", {
        mp3: jQuery('#jquery_jplayer').attr('media_file')
      });
    },
    swfPath: "/js/jplayer",
    supplied: "mp3"
  });
});

it seems like the productAddToCartForm is not getting instantiated似乎 productAddToCartForm 没有被实例化

You could try to use firebug to create a VarienForm object from the console to see if its returning an object.您可以尝试使用 firebug 从控制台创建 VarienForm object,看看它是否返回 object。

var tmpobj = new VarienForm('product_addtocart_form') var tmpobj = new VarienForm('product_addtocart_form')

and see what what gets created in tmpobj看看在 tmpobj 中创建了什么

And also the.bind after both functions does not seems ok.而且这两个函数之后的.bind 似乎也不行。 You are not using an inmediate function and besides does functions are not returning anything.您没有使用中间 function 并且函数没有返回任何内容。

A more complete example would be easier to fix.更完整的示例将更容易修复。

I hope this helps you.我希望这可以帮助你。

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

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