简体   繁体   English

jQuery / Javascript显示使用一个按钮隐藏多个内容?

[英]JQuery / Javascript Show Hide multiple Content using one button?

I got a requirement like this, 我有这样的要求

<p id="admore">Add More</p>
<span id="qulification1">
       Qulification 1<input type="text" name="qulification[]"/>
</span>
<br/>
<span id="qulification2" style="display: none">
    Qulification 2<input type="text" name="qulification[]"/>
    <span id="remove2">remove</span>
</span>
<br/>
<span id="qulification3" style="display: none">
    Qulification 3 <input type="text" name="qulification[]"/>
    <span id="remove3">remove</span>
</span>

qulification2 and qulification3 span default keep hide, when click one time on "Add More" btn, it shows qulification2. qulification2和qulification3跨度默认保持隐藏,在“添加更多” btn上单击一次时,将显示qulification2。 next click shows qulification3. 下一步单击显示qulification3。 How to do it? 怎么做?

I have done it using two Add More button. 我使用两个“添加更多”按钮来完成此操作。 but it is not good way, 但这不是好方法

<p id="admore2">Add More</p>
<p id="admore3">Add More</p>

$('#addmore2').click(function() {
    $('#qulification2').show();
});

$('#addmore3').click(function() {
    $('#qulification3').show();
});

The easiest way to do this is with class selectors, this way on each click one more appears of those spans: 最简单的方法是使用类选择器,每单击一次,这些范围就会再次出现:

css: .invisible { display: none; } CSS: .invisible { display: none; } .invisible { display: none; }

html: on every span, should be an extra class for grouping and another for invisibility: html:在每个范围上,都应该是一个用于分组的额外类,另一个是用于隐身的类:

<span id="qulification1" class="q invisible">

js: js:

$('#addmore').click( function() { $('span.q.invisible').first().removeClass('invisible'); } );

You can just do : 您可以这样做:

var counter = 0;

$('#addmore2').click(function() {
    if (count === 0) {
        $('#qulification2').show();
    } else if (count == 1) {
        $('#qulification3').show();
    }
    count++;
});

Of course there are many way to solve the problem you are encountering. 当然,有很多方法可以解决您遇到的问题。 About the most simple way to solve this issue is to add a counter to your program which will count how many times you have pressed the button. 解决此问题的最简单方法是在程序中添加一个计数器,该计数器将计算您按下按钮的次数。 If you want to only be able to show the content and not hide it again, you can use the following code: 如果只希望显示内容而不希望再次隐藏它,则可以使用以下代码:

var count = 0;

$('#addmore2').click(function() {
    if (count == 0) {
        $('#qulification2').show();
    }
    else if (count == 1) {
        $('#qulification3').show();
    }
    count++;
});

However, there may be a better approach to your problem. 但是,可能有更好的方法来解决您的问题。 If you wish to be able to show the content and then be able to hide it once it is all on the screen, you can use the following code: 如果您希望能够显示内容然后在屏幕上全部隐藏之后将其隐藏,则可以使用以下代码:

var count = 1;

$('#addmore2').click(function() {
    if (count == 1) {
        $('#qulification2').show();
    }
    else if (count == 2) {
        $('#qulification3').show();
    }
    else if (count == 3) {
        $('#qulification2').hide();
        $('#qulification3').hide();
        count = 0;
    }
    count++;
});

Check the visibility of the spans and show the first one hidden: 检查跨度的可见性并显示第一个隐藏的:

$('#addmore').click(function() {
    if ( $('#qulification2').css('display') == 'none' )                                     
          $('#qulification2').show();
   else if ( $('#qulification3').css('display') == 'none' )                                     
          $('#qulification3').show();});
}) ;

What you really should do is pragmatically insert another node after user click "Add" button. 您真正应该做的是在用户单击“添加”按钮后实用地插入另一个节点。

Give your whole thing a container, just append to it. 给您的整个容器一个容器,只需附加它即可。 For example, 例如,

 $("#AddButton").click(
function()
{
$("#Container").append("<span>your item stuff</span>");
}

);

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

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