简体   繁体   English

单击按钮后如何使用 jQuery 仅显示一个 div

[英]How to show only one div with jQuery after clicking on a button

I have two divs which have a display: none on CSS.我有两个有display: none CSS 上没有。 Also, I have a jQuery code to a button, which when is clicked, will show one of the divs.另外,我有一个按钮的 jQuery 代码,单击该按钮时,将显示其中一个 div。 It depends if one of the input type='radio' is :check .这取决于input type='radio'之一是否为:check For this, I have eight input radios and if the correct ones are checked, It is supposed to show the #msgCmnhCer message.为此,我有八个输入收音机,如果检查了正确的收音机,它应该显示#msgCmnhCer 消息。 Else, if the wrong inputs are checked, It have to show the #msgCmnhErr message.否则,如果检查了错误的输入,它必须显示 #msgCmnhErr 消息。

The problem is: when I select the wrong inputs, the error message appears as expected.问题是:当我 select 输入错误时,错误信息按预期出现。 But, when I select the correct inputs, both of the divs appears.但是,当我 select 输入正确时,两个 div 都会出现。

I tried to do it with jQuery.我试着用 jQuery 来做。 The code is below代码如下

$("#exec").click(function(){

    // frenteF1 and andar5F1 are the IDs of the correct inputs. below is to show the correct message
    if(($('.frenteF1').is(':checked')) && ($('.andar5F1').is(':checked'))){
        $("#msgCmnhCer").css("display", "block");
        $('#msgCmnhCer').fadeOut(4500);
    }
    // error message
    else{
        $('#msgCmnhErr').css("display", "block");
        $('#msgCmnhErr').fadeOut(4500);

    }
});

Here is the HTML code of the messages这是消息的 HTML 代码

                <div id="msgCmnhCer" class="msg-cmnh-certo">
                    <p>
                        You got it!
                    </p>
                </div>
                <div id="msgCmnhErr" class="msg-cmnh-errado">
                    <p>
                        Try again.
                    </p>
                </div>

Here are the input buttons code:以下是输入按钮代码:

      <div class="qst1">
         <input type="radio" name="imagem" id="d1" class="input-fase1 direitaF1"/>
         <label for="d1" class="label-fase1 prgt-medieval">Right</label>
                                
         <input type="radio" name="imagem" id="d2" class="input-fase1 esquerdaF1"/>
         <label for="d2" class="label-fase1 prgt-medieval">Left</label>
                                
         <input type="radio" name="imagem" id="d3" class="input-fase1 frenteF1"/>
         <label for="d3" class="label-fase1 prgt-medieval">Ahead</label>
                                
         <input type="radio" name="imagem" id="d4" class="input-fase1 trasF1"/>
         <label for="d4" class="label-fase1 prgt-medieval">Behind</label>
      </div>
            
      <div class="qst1">
         <input type="radio" value="andar1" name="image" id="a1" class="input-fase1 andar2F1"/>
         <label for="a1" class="label-fase1 prgt-medieval">(2)</label>
                                
         <input type="radio" name="image" id="a2" class="input-fase1 andar3F2"/>
         <label for="a2" class="label-fase1 prgt-medieval"> (3)</label>
                                
         <input type="radio" name="image" id="a3" class="input-fase1 andar4F1"/>
         <label for="a3" class="label-fase1 prgt-medieval"> (4)</label>
                                
         <input type="radio" name="image" id="a4" class="input-fase1 andar5F1"/>
         <label for="a4" class="label-fase1 prgt-medieval"> (5)</label>
      </div>

How could I make only the correct message appear if the right inputs are checked, without the error message?如果检查了正确的输入,我怎样才能只显示正确的消息,而没有错误消息?

It is impossible for the code for the if condition to be false AND true. if 条件的代码不可能为假和真。 The code will not execute both code blocks on the same pass.代码不会在同一次传递中执行两个代码块。 One of two things may be happening here:这里可能会发生两件事之一:

(1) Somewhere, $("#exec").click(...) is triggered more than once, and somehow the if condition is true the first time and false the second time. (1) 在某处,$("#exec").click(...) 被多次触发,不知何故,if 条件第一次为真,第二次为假。

(2) You clicked it the first time and the if condition was true, then you didn't wait for the fadeout and clicked again quickly when the if condition was false. (2)第一次点击if条件为真,不等fadeout,if条件为假时再次快速点击。

It seems that you may be actually referring to checkboxes, not radio boxes.看来您实际上可能指的是复选框,而不是单选框。

https://jsfiddle.net/3v0bxqsw/ https://jsfiddle.net/3v0bxqsw/

 <div id="msgCmnhCer" class="msg-cmnh">
   <p>
     You got it!
   </p>
 </div>
 <div id="msgCmnhErr" class="msg-cmnh">
   <p>
     Try again.
   </p>
 </div>
 
<input type="checkbox" class='andar5F1' id="male" name="gender" value="male" />
<label for="male">Male</label><br>
<input type="checkbox" id="female" class='frenteF1' name="gender" value="female" />
<label for="female">Female</label><br>
<input type="checkbox" id="other" name="gender" value="other" />
<label for="other">Other</label>
<button id="exec">
Click me
</button>

I'm assuming that the two radio buttons are part of different groups, and hence both can be selected at the same time.我假设这两个单选按钮是不同组的一部分,因此可以同时选择两者。 If they are part of the same question/group, the condition would always be false, as only once can be selected at any point in time.如果他们是同一问题/组的一部分,则条件将始终为假,因为在任何时间点只能选择一次。

You didn't provide the exact HTML structure, so I made a quick JSFiddle with some dummy html.你没有提供确切的 HTML 结构,所以我用一些虚拟 html 做了一个快速的 JSFiddle。 The javascript works correctly for me in the example: https://jsfiddle.net/7m5L6yqo/13/ javascript 在示例中为我正常工作: https://jsfiddle.net/7m5L6yqo/13/

JS Code: JS代码:

$("#exec").click(function() {
      if ((($('.frenteF1').is(':checked')) && ($('.andar5F1').is(':checked')))) {
        $("#msgCmnhCer").css("display", "block");
        $('#msgCmnhCer').fadeOut(4500);
      }
      else {
        $('#msgCmnhErr').css("display", "block");
        $('#msgCmnhErr').fadeOut(4500);
      }
});

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

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