jQuery ajax按钮onclick事件不起作用

时间:2019-11-23 16:33:36

标签: jquery html ajax onclick

我有2个按钮,一个按钮减少数量,一个按钮增加数量。按钮的html代码是这样的。

<button id='plusqty' type='button' value='".$row['idshoppingcart']."' class='btn btn-default btn-number' >+</button>
<button id='minusqty' type='button' value='".$row['idshoppingcart']."' class='btn btn-default btn-number' >-</button>

以及减号按钮的ajax代码

$("#minusqty").click(function(){
        var action = 'data';
        var type = '-';
        $.ajax({            
            url:'changeqty.php',
            method: 'POST',
            data:{action:action,type:type,id:$(this).val()},
            success:function(response){
                $("#result").html(response);

            }
        });
    });

我对网站上的其他按钮使用了相同的ajax方法,例如“添加到购物车”功能,并且效果很好。但是,这尤其不起作用。我单击“ +”按钮,甚至什么都没有发生,这使我相信jquery侦听器没有触发。有什么建议吗?非常感谢!

编辑:正如有人提到的那样,我将ajax侦听器更改为类选择器,但是仍然无法正常工作。

2 个答案:

答案 0 :(得分:2)

您正在使用类选择器:

  

$(“。minusqty”)。click(function(){

使用ID选择器代替:

$("#minusqty").click(function(){

答案 1 :(得分:1)

$(document).on('click','#minusqty',function()

});