私はカードのような複数の選択divを持っています。 「選択」をクリックすると、そのカードのタイトルが選択リストに追加されます。JQuery toggle()はHTML要素を追加します
私の問題は「unselect」ボタンで、リストからカードのタイトルを削除することです。私はtoggle()
とremove()
を試してみました。カードタイトルは追加されますが削除されません。
function accessories(){
var accessories;
$('.accessories').toggle(function() {
accessories = $(this).closest('.card').find('.image-title').text();
acc_txt = '<p> - ' + accessories + '</p>'; //element to append and remove later.
$('#selection').append(acc_txt);
$(this).css({background: '#35a8a5', border: '1px solid #35a8a5'});
$(this).val('Unselect');
}, function() {
$('#selection').remove(acc_txt);
$(this).css({background: '', border: ''});
$(this).val('Select');
});
}
jQuery(document).ready(function() {
accessories();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-4 card">
<div class="card-img">
<img src="http://skiersedge.com.sg/wp-content/uploads/2016/08/cpw-vest.jpg"/>
</div>
<h3 class="image-title text-center">Core Power Weighted Vest™</h3>
<div class="image-desc">
<p><strong>Available for all models.</strong></p>
<p>Increase the intensity of any workout with the addition of the Core Power weighted vest with soft flexible weights. This is the best fitting, most comfortable weighted vest available and is adjustable up to 22 lbs. in 1/2 lb. increments. Comes standard with 11 lbs.</p>
</div>
<input type="button" name="next" class="accessories" value="Select"/>
</div>
<div id="selection">here Should be appended the cards</div>
.accessories
カード内のボタンのクラスです。だから私はタイトル.image-title
を検索し、それを追加します。このページには複数のカードがあります。 私はどのように私は非選択ボタンの作品を作ることができますか分からない。 どうすれば実現できますか?
あなたが遭遇している挙動を示している(それは、エディタ上のボタンの一つだ)スタックスニペットを提供することができますか? – Tschallacka
また、あなたのHTML構造を共有してください。より良いもの:JSFiddleを作成してください。 – LinkinTED
要素を追加したり削除したりする際に、イテレータ、すなわち[index]を使用する方法があります。 '' '$( '#selection')' 'セレクタはすべてのマークアップを返します。これが助けてくれることを願っています –