簡體   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