简体   繁体   English

jQuery onclick 方法不起作用两次

[英]jQuery onclick method doesn't work twice

I have 2 onclick methods.我有 2 个 onclick 方法。 The first one works fine but the second one doesn't works if i click first one, I need to refresh the page to run.第一个工作正常,但如果我单击第一个,第二个不起作用,我需要刷新页面才能运行。 Even if I do that, it only works once.即使我这样做,它也只能工作一次。

    $(document).ready(function(){
    $(".cart_add").on('click','.add_cart',function(){
        var product_id = $(this).data("productid");
        var size = $('ul#size').find('li.active a').data('value');
        var qty = $('.qty-input').val();
        var price = $('.special-price').data('price');
        var name = $('h1.product-name').text();
        var url = $(this).data("url");
        $.post(url,{product_id:product_id,product_size:size,product_name:name,product_price:price,product_qty:qty},function(response){
            $(".variant-1").html(response);
        });
    });

    $(".secondary").on('click','.delete',function(){
        var product_id = $(this).data("id");
        var url = $(this).data("url");
        $.post(url,{product_id:product_id},function(response){
            $(".variant-1").html(response);
        });
    });
})

 $("#CheckClick").click(function(){ alert("The paragraph was clicked."); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success" id="CheckClick">button</button>

Take ID Instead of Class check in side your code or take your script inside function and call it on button click以 ID 而不是类检查您的代码或将您的脚本带入函数并在按钮单击时调用它

 function myFunction(){ alert("it's work well") }
 <input id="clickMe" type="button" value="clickme" onclick="myFunction();" />

you can try using onclick event from html button like`您可以尝试使用来自 html 按钮的 onclick 事件,例如`

 function myFunction() { alert('"hello"'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="myFunction()">Click me</button>

e` e`

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

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