简体   繁体   English

隐藏div后,允许在单击按钮时重新显示

[英]After hiding div allow to redisplay on button click

The initial display is to only have the first div displaying. 初始显示将仅显示第一个div。 When you select Add Another, the next div will display as well. 当选择添加另一个时,下一个div也将显示。 When you select "Remove" next to that div it will be hidden/removed. 当您选择该div旁边的“删除”时,它将被隐藏/删除。

The problem I am having is how to get the div to redisplay if the user selects Add Another. 我遇到的问题是,如果用户选择“添加另一个”,如何使div重新显示。 So that they can have a max of 4 divs displaying. 这样它们最多可以显示4个div。 Currently after selecting "Remove" if you select "Add Another" it will only display a div if it has not been displayed and removed previously. 当前,如果您选择“添加另一个”,则在选择“删除”后,如果以前未显示并删除它,它只会显示一个div。 I would like the user to always have the option of adding all 4 divs even if they were to add one then remove it and then would like to add it back. 我希望用户始终可以选择添加所有4个div,即使他们要添加一个然后删除它,然后又将其添加回去。

Here is my current code: 这是我当前的代码:

 //Remove div when clicked $('#donation_element1_row').show(); $('.removeDiv').click( function() { $(this).closest('div[id*="donation_element"]').hide(); }); //Add another designation var count = 0, $allDivs = $('#donation_element2_row, #donation_element3_row, #donation_element4_row'); $('#addAnother').click( function() { if( count < $allDivs.length ) { $allDivs.eq( count ).fadeIn( 'slow' ); count++; } }); 
 #addAnother { display: inline-block; width: 236px; height: 50px; line-height: 44px; text-align: center; border: 3px solid #f8a61b; color: #860005; font-size: 16px; font-weight: bold; margin-bottom: 0px; margin-top: 20px; cursor: pointer; background-color: #f8a61b; } div[id*='donation_element'] { background-color: blue; color: #fff; display: none; margin: 10px; max-width: 200px; } .removeDiv { cursor: pointer; text-align: right; background-color: #333; text-align: center; } p { text-align: center; padding-top: 10px; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="donation_element1_row"> <p>Div 1</p> <div class="removeDiv">Remove X</div> </div> <div id="donation_element2_row"> <p>Div 2</p> <div class="removeDiv">Remove X</div> </div> <div id="donation_element3_row"> <p>Div 3</p> <div class="removeDiv">Remove X</div> </div> <div id="donation_element4_row"> <p>Div 4</p> <div class="removeDiv">Remove X</div> </div> <div id="addAnother">+ Add Another</div> 

You can also use the below code. 您也可以使用以下代码。 So when you delete the div, append it to the end. 因此,当您删除div时,请将其附加到末尾。 This will help you for more than 4 divs. 这将帮助您超过4个div。

 //Remove div when clicked $('#donation_element1_row').show(); $('.removeDiv').click( function() { $(this).parent().hide(); $("#parent_donation_container").append($(this).parent()); }); $('#addAnother').click( function() { $(".donation_element").not(":visible").first().fadeIn( 'slow' ); }); 
 #addAnother { display: inline-block; width: 236px; height: 50px; line-height: 44px; text-align: center; border: 3px solid #f8a61b; color: #860005; font-size: 16px; font-weight: bold; margin-bottom: 0px; margin-top: 20px; cursor: pointer; background-color: #f8a61b; } div[id*='donation_element'] { background-color: blue; color: #fff; display: none; margin: 10px; max-width: 200px; } .removeDiv { cursor: pointer; text-align: right; background-color: #333; text-align: center; } p { text-align: center; padding-top: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="parent_donation_container"> <div id="donation_element1_row" class="donation_element"><p> Div 1</p><div class="removeDiv">Remove X</div></div> <div id="donation_element2_row" class="donation_element"><p> Div 2</p><div class="removeDiv">Remove X</div></div> <div id="donation_element3_row" class="donation_element"><p> Div 3</p><div class="removeDiv">Remove X</div></div> <div id="donation_element4_row" class="donation_element"><p> Div 4</p><div class="removeDiv">Remove X</div></div> </div> <div id="addAnother">+ Add Another</div> 

https://jsfiddle.net/fye8qjk1/11/ https://jsfiddle.net/fye8qjk1/11/

You're almost there. 你快到了。 When you remove an item you also need to increment the count down, and add the first div to the list of divs. 删除项目时,还需要递减计数,并将第一个div添加到div列表中。 For example: 例如:

$('.removeDiv').click( function() {
  $(this).closest('div[id*="donation_element"]').hide();
  count --;
});

$allDivs = $('#donation_element1_row, #donation_element2_row, #donation_element3_row, #donation_element4_row');

Fiddle here: https://jsfiddle.net/ofw4g2ym/2/ 在这里提琴: https : //jsfiddle.net/ofw4g2ym/2/

You don't need any counter, 您不需要任何柜台,
make your code flexible to account for any number of donation elements 使您的代码可以灵活地处理任意数量的捐赠元素

 var $donation = $(".donation"); var $donationAdd = $(".donation-add"); var $donationRemove = $(".donation-remove"); var donationTot = $donation.length; function donationHandler () { if ($donation.filter(":visible").length >= donationTot) { $donationAdd.prop("disabled", true); } } $donationAdd.on("click", function() { $donation.filter(":hidden").eq(0).show(); donationHandler(); }); $donationRemove.on("click", function() { $(this).closest(".donation").hide(); donationHandler(); }); 
 *{margin:0; box-sizing:border-box;} .donation { padding: 10px; background: #eee; margin-bottom: 10px; } .donation ~ .donation{ display: none; } 
 <div class="donation"> <p>Div 1</p> <button type="button" class="donation-remove">&times; Remove</button> </div> <div class="donation"> <p>Div 2</p> <button type="button" class="donation-remove">&times; Remove</button> </div> <div class="donation"> <p>Div 3</p> <button type="button" class="donation-remove">&times; Remove</button> </div> <div class="donation"> <p>Div 4</p> <button type="button" class="donation-remove">&times; Remove</button> </div> <button type="button" class="donation-add">+ Add</button> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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