2016-02-18 57 views
1

我有一個列表,其中包含兩種不同的項目,product-x和product-y。jQuery - 刪除'對應'項目

當用戶刪除產品x我需要它的等效產品y也刪除和反之亦然。

我已經嘗試了這兩種不同的方式,但似乎沒有工作。我感謝任何幫助!


JS

$(document).on('click', '.delete-item', function(e) { 
    $(this).parents('.product-x').delay(500).remove(); 
    $(this).parents('.product-y').delay(500).remove(); 
}); 

CSS

.product-x, .product-y{ 
    width:100px; 
    height:50px; 
    margin:10px; 
} 

.product-x{ 
    border:red 1px solid; 
} 

.product-y{ 
    border:navy 1px solid; 
} 

HTML

<div id="product-x-wrapper"> 
    <div class="product-x">1 
      <button class="delete-item">x</button> 
     </div> 
     <div class="product-x">2 
      <button class="delete-item">x</button> 
     </div> 
     <div class="product-x">3 
      <button class="delete-item">x</button> 
     </div> 
    </div> 
<div id="product-y-wrapper"> 
     <div class="product-y">1 
      <button class="delete-item">x</button> 
     </div> 
     <div class="product-y">2 
      <button class="delete-item">x</button> 
     </div> 
     <div class="product-y">3 
      <button class="delete-item">x</button> 
     </div> 
</div> 
+0

當你說「兄弟姐妹」時,你是指具有相同數字的人嗎? (例如1,2或3) – Pabs123

+0

您沒有正確使用術語「兄弟」。在這種情況下,「兄弟」是與所討論的元素處於相同DOM級別/相鄰的ANY元素。你想要刪除相同的「產品y」項目,假設 - 如果它是#2,那麼刪除#2產品y? –

+0

'兄弟'你真的相對應嗎? – j08691

回答

4

首先,我想補充一個data-product-id到每一個元素,所以你可以找到他們不依靠人類可讀標籤:

<div id="product-wrapper"> 
    <div class="product-x" data-product-id="1">1 <button...></div> 
    <div class="product-x" data-product-id="2">2 <button...></div> 
    <div class="product-x" data-product-id="3">3 <button...></div> 
    <div class="product-y" data-product-id="1">1 <button...></div> 
    <div class="product-y" data-product-id="2">2 <button...></div> 
    <div class="product-y" data-product-id="3">3 <button...></div> 
</div> 

然後你就可以刪除匹配的兄弟姐妹是這樣的:

$(document).on('click', '.delete-item', function(e) { 
    var id = $(this).parent().data('product-id'); 
    $("#product-wrapper").find('div[data-product-id="' + id + '"]').delay(500).remove(); 
}); 

編輯:既然你已經改變了將產品分成兩個包裝的問題,你可以用這個辦法,但單從兩地刪除的東西,就像這樣:

$(document).on('click', '.delete-item', function(e) { 
    var id = $(this).parent().data('product-id'); 
    $("#product-x-wrapper").find('div[data-product-id="' + id + '"]').delay(500).remove(); 
    $("#product-y-wrapper").find('div[data-product-id="' + id + '"]').delay(500).remove(); 
}); 
+0

這是正確的答案,他應該只是使用適當的ID –

+0

,但你可能只是擺脫了數據,只是做id ='product-x-1' –

+0

這很好,但如果產品包含在兩種不同的包裝中,我將如何解決? – unfollow

0

你應該只需要添加一個ID,所以你可以通過ID查找它,但假設他們都相應的,爲了你能做到這一點

$(document).on('click', '.delete-item', function(e) { 
    var producty = $(this).parent(); 
    var index = $(".product-y").index(producty); 

    $('.product-x:eq('+ index +')').remove(); 
} 

這也讓你的HTML不同

0

我會這樣做的方式是獲取使用jquerys .val()單擊的product-y的值。然後,您可以遍歷所有產品的x,一旦找到匹配的.val,那麼您可以刪除它。

如果需要,我可以給你寫一些代碼,但希望這足以幫助你理解如何解決它。

E/- 保羅的辦法是更優雅的使用是:d

+0

「如果你需要,我可以給你寫一些代碼」 - 這通常是答案應該包括的內容。 – j08691

+0

@ j08691這是一個相當簡單的問題,我假設一個可靠的解釋可能會更有利於嘗試編碼自己,以便爲他簡單地做,但是採取了點。 –

0

快速注:應使用某種形式的DOM的句柄,可訪問使用選擇,像classiddata- attributesname

現在如果你仍然想這樣,我強烈不鼓勵,那麼你可以使用:contains選擇器。

$(document).on('click', '.delete-item', function(e) { 
    $content = $(this).parent().html(); 
    $(this).parents('.product-x').delay(500).remove(); 
    $(".product-y:contains("+$content+")).delay(500).remove(); 
}); 
0

稍有不同的看法:

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 

<div data-product="one" class="one product-x">1 
    <button class="delete-item">x</button> 
</div> 
<div data-product="two" class="two product-x">2 
    <button class="delete-item">x</button> 
</div> 
<div data-product="three" class="three product-x">3 
    <button class="delete-item">x</button> 
</div> 
<div data-product="one" class="one product-y">1 
    <button class="delete-item">x</button> 
</div> 
<div data-product="two" class="two product-y">2 
    <button class="delete-item">x</button> 
</div> 
<div data-product="three" class="three product-y">3 
    <button class="delete-item">x</button> 
</div> 

<script type="text/javascript"> 
$('.delete-item').on('click',function(e){ 
    var theProduct = $(this).parent().data('product'); 
    var theElements = $('div[data-product="' + theProduct + '"]'); 
    theElements.delay(500).fadeOut(); 
}) 
</script> 

淡出()是性感;)

1

無需任何額外的標記或類,你可以使用jQuery的.index()功能刪除相應的項目:

$('.delete-item').each(function() { 
 
    $(this).parent().data('idx', $(this).parent().index()) 
 
}).click(function() { 
 
    var idx = $(this).parent().data('idx'); 
 
    $('div > div').each(function() { 
 
    if ($(this).data('idx') === idx) $(this).remove() 
 
    }) 
 
})
.product-x, 
 
.product-y { 
 
    width: 100px; 
 
    height: 50px; 
 
    margin: 10px; 
 
} 
 
.product-x { 
 
    border: red 1px solid; 
 
} 
 
.product-y { 
 
    border: navy 1px solid; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="product-x-wrapper"> 
 
    <div class="product-x">1 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
    <div class="product-x">2 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
    <div class="product-x">3 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
</div> 
 
<div id="product-y-wrapper"> 
 
    <div class="product-y">1 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
    <div class="product-y">2 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
    <div class="product-y">3 
 
    <button class="delete-item">x</button> 
 
    </div> 
 
</div>