简体   繁体   English

添加到购物车按钮-Javascript / JQuery错误

[英]Add to cart button - Javascript/JQuery error

When webpage loads: 网页加载时:

Invalid App Id: Must be a number or numeric string representing the application id. 无效的应用程序ID:必须是代表应用程序ID的数字或数字字符串。

When Add to Cart button is clicked: 单击添加到购物车按钮时:

Uncaught TypeError: Cannot read property 'id' of undefined - (index):459 未被捕获的TypeError:无法读取未定义的属性“ id”-(index):459

Below is the Javascript, I have marked the area with "ERROR LINE" at (index):459 以下是Javascript,我在(index):459用"ERROR LINE"标记了该区域

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;
        if ($(button).id.indexOf('ec_shortcut') != -1) { // -ERROR LINE
            try {
                this.form.submit();
                return;
            } catch (e) {}
        }
        if (!url) {
            url = jQuery('#product_addtocart_form').attr('action');
        }
        url = url.replace("checkout/cart", "oxajax/cart");
        url = url.replace("wishlist/index/cart", "oxajax/cart/add");
        var data = jQuery('#product_addtocart_form').serialize();
        data += '&isAjax=1';
        if ('https:' == document.location.protocol) {
            url = url.replace('http:', 'https:');
        }
        jQuery.fancybox.showActivity();
        jQuery.ajax({
            url: url,
            dataType: 'jsonp',
            type: 'post',
            data: data,
            success: function(data) {
                Olegnax.Ajaxcart.helpers.showMessage(data.message);
                Olegnax.Ajaxcart.helpers.cartSuccessFunc(data);
            }
        });
        this.form.action = oldUrl;
        if (e) {
            throw e;
        }
    }
}.bind(productAddToCartForm);

$(button).id - Error Cause $(button).id错误原因

$(button) is a jQuery object, use $(button)[0].id or $(button).prop('id') or just button.id $(button)是一个jQuery对象,使用$(button)[0].id$(button).prop('id')或仅使用button.id

Assuming button is a DOMElement you can use: 假设按钮是可以使用的DOMElement:

if (button.id.indexOf('ec_shortcut') != -1) {

Alternatively, you can get the id property from the jQuery object using prop() : 另外,您可以使用prop()从jQuery对象获取id属性:

if ($(button).prop('id').indexOf('ec_shortcut') != -1) {

试试这个: button.id.indexOf('ec_shortcut')

use attr to get the id value 使用attr获取id值

$(button).attr('id');

NOTE: You can get any attribute value using this method 注意:您可以使用此方法获取任何属性值

See jQuery Docs for more info 有关更多信息,请参见jQuery Docs

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

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