在包含多个元素的选择器上执行jQuery.click

时间:2017-10-23 06:51:17

标签: javascript jquery html liquid

我有两个ID为&#34的标识按钮;数据侧删除":
我还有以下jQuery函数



    $('#data-side-remove').click(function(){
      $(this).closest('.line-item-info').slideUp("slow");
      window.location.href = $(this).attr("data-href-remove");
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button
        id="data-side-remove"
        class="plus-box"
        data-href-remove="/cart/change?line={{ forloop.index }}&amp;quantity=0"
        type="button"
        name="button">x</button>
&#13;
&#13;
&#13;

但由于某种原因,代码只为两个按钮中的第一个工作正常。然后,一旦它由于slideUp函数而消失,代码也适用于第二个按钮,依此类推。我想这是一个事实,一旦它被选中,$('#data-side-remove')成为一个数组,而该函数正在处理数组的第一个元素。解决这个问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

  

$(this)将拉出你点击的唯一加号框的href

    $('.plus-box').click(function(){
      $(this).closest('.line-item-info').slideUp("slow");
      window.location.href = $(this).attr("data-href-remove");
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button
        id="data-side-remove"
        class="plus-box"
        data-href-remove="/cart/change?line={{ forloop.index }}&amp;quantity=0"
        type="button"
        name="button">x</button>