2017-03-07 83 views
1

我们正在运行Opencart 1.5.6.4,它使用以下代码将项目添加到购物车。JavaScript onclick事件不适用于移动设备

<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'); 
       } 
      } 
     }); 
    } 
+0

在哪个手机浏览器中onclick不起作用? – Jer

+0

Chrome/Safari - 来自iPhone和Android –

回答

0

阿贾克斯是失败原因到www。从网站URL丢失。

-2

尝试使用AJAX返回false停止冒泡。

0

检查此函数是否运行。也许放置一些console.log。

关于@ volodymyr-duday anwser:不是返回false,而是事件对象stopPropagation()中有一个方法。

如果函数无法运行,请尝试将其附加到触摸事件,如touchend

1

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

$("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更多的参考。

+0

嗨,感谢您的回答,不幸的是您的答案完全打破了按钮。 –

+0

这不应该发生。你尝试调试吗? –

+0

这对我有效,谢谢 – Eoiner

0

我可以看到你使用jQuery。 如果点击事件不火,你可以尝试

$(document).ready(function(){ 
    $("[your-selector-here]").on("click touchstart", function(){ 
     //do stuff 
    }); 
}); 

否则,我看你需要在你的方法,但在你的事件2点的参数,你只能有一个

function addToCart(product_id, quantity){} 

onclick="addToCart('<?php echo $product['product_id']; ?>');" 
+0

也许这个特定的购物车项目不需要,他使用默认值的第一行功能。 –

相关问题