JavaScript onclick 事件在移动设备上不起作用

标签 javascript html onclick opencart

我们正在运行 Opencart 1.5.6.4,它使用以下代码将商品添加到购物车。

<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />

我们注意到,在移动设备上,Javascript onclick 事件似乎不起作用。

javascript函数如下

function addToCart(product_id, quantity) {
    quantity = typeof(quantity) != 'undefined' ? quantity : 1;

        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=' + quantity,
            dataType: 'json',
            success: function(json) {
                $('.success, .warning, .attention, .information, .error').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['success']) {
                    $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                    $('.success').fadeIn('slow');

                    $('#cart-total').html(json['total']);

                    $('html, body').animate({ scrollTop: 0 }, 'slow'); 
                }   
            }
        });
    }

最佳答案

尝试将触摸事件添加到您的代码中。

$("input.button").on("click touchend", function () {
    addToCart($(this).attr("data-product-id"));
});

function addToCart(product_id, quantity) {
    quantity = typeof(quantity) != 'undefined' ? quantity : 1;

    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: 'product_id=' + product_id + '&quantity=' + quantity,
        dataType: 'json',
        success: function(json) {
            $('.success, .warning, .attention, .information, .error').remove();

            if (json['redirect']) {
                location = json['redirect'];
            }

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({
                    scrollTop: 0
                }, 'slow');
            }
        }
    });
}

html:

<input type="button" value="<?php echo $button_cart; ?>" data-product-id="<?php echo $product['product_id']; ?>" class="button" />

您可以找到有关触摸事件的更多引用 here .

关于JavaScript onclick 事件在移动设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650098/

相关文章:

javascript - 这个javascript可以减少吗?

javascript - Cocos2d html5 onTouchEnded 不工作

javascript - 我怎样才能用 Javascript 或 JQuery 做到这一点

javascript - 如何以 Angular 6 显示垫表中每一行的垫旋转器

html - 我可以将文本直接放在<div>...</div> 中吗?或者我需要先将它包装在 <p>...</p> 中吗?

javascript - 即使设置了滚动功能,暂停按钮也会暂停

android - 点击事件转移到下一个启动的Activity

javascript - 使用 for 循环将 onclick 方法附加到多个 Canvas

javascript - JSF2.0 - CSS-Change 在生命周期中丢失

javascript - 需要帮助找到用于在网页之间连续循环的 setTimeout 函数的剩余时间