繁体   English   中英

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

[英]Execute jQuery.click on a selector that contains multiple elements

我有两个标识为“ data-side-remove”的相同按钮:
我也有以下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> 

但是由于某种原因,代码仅适用于两个按钮中的第一个。 然后,由于使用了slideUp功能而消失了,该代码也适用于第二个按钮,依此类推。 我想这与以下事实有关:选择它后, $('#data-side-remove')成为一个数组,并且该函数仅在该数组的第一个元素上工作。 解决此问题的最佳方法是什么?

$(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> 

暂无
暂无

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

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