2017-03-07 128 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

也許這個特定的購物車項目不需要,他使用默認值的第一行功能。 –

相關問題