2017-03-07 15 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イベントが機能していないことがわかりました。

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

持っているあなたは、あなたの方法ではなく、あなたのイベントの2つの引数を必要と見ますwwwに。ウェブサイトのURLから欠落しています。

-2

てみ使用がバブリングを停止するAJAX後に偽を返す次のように

javascript関数です。

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 
    }); 
}); 

を試すことができ、私はあなたが唯一のAjaxが原因失敗していた1

function addToCart(product_id, quantity){} 

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

多分、この特定のカートアイテムは、彼は機能の最初の行でデフォルト値を使用する必要はありません。 –

関連する問題