简体   繁体   English

隐藏/显示表单,取决于发送的输入

[英]hide/show forms depending on what the input is send

I have 2 forms on a page. 我在页面上有2个表格。 I need to separate this via input password. 我需要通过输入密码将其分开。 If the user types "1" then submit the page display 1 set of form, if "2" is the input then submit the 2nd set of the form. 如果用户键入“ 1”,则提交页面显示1套表单,如果输入“ 2”,则提交第二套表单。

Not quite sure if this is doable on Javascript or in jQuery. 不确定在Javascript或jQuery中是否可行。 I'm using WordPress as a CMS. 我正在使用WordPress作为CMS。 If you know a plugin that can solve the issue that would be good. 如果您知道可以解决该问题的插件,那将会很好。

HTML: HTML:

<p>ENTER YOUR INVITATION CODE</p>
<div id="incorrect">Sorry, Try again!</div>
<input type='text' id="code"/>
<input type='button' id="buttontest" value="VERIFY"/>

<div id="JPC1">CONTENT 1</div>
<div id="JPC2">CONTENT 2</div>

SCRIPT: 脚本:

$(document).ready(function(){
    $("#buttontest").click(function(){
        if ($('#code').val() == "1") {   
            $("#JPC1").show("fast"); //Slide Down Effect
            $("#JPC2").hide("fast"); //Slide Down Effect
            $("#incorrect").hide("fast");    //Slide Up Effect
        } 
        if ($('#code').val() == "2") {   
            $("#JPC1").hide("fast"); //Slide Down Effect
            $("#JPC2").show("fast"); //Slide Down Effect
            $("#incorrect").hide("fast");    //Slide Up Effect
        } 
        else {
            $("#incorrect").show("fast");    //Slide Up Effect
        }
    });
});

STYLE: 样式:

#incorrect {display:none;} #code {color:#000;}
#JPC1, #JPC2 {display:none;}

SAMPLE FIDDLE: FIDDLE 样本字段: 字段

you need to add else if for second check take a look fiddle i have update for you 如果需要第二次检查,请添加其他东西,让我看一下小提琴

enter code here fiddle enter code here 捣鼓

Try this : 尝试这个 :

$(document).ready(function(){
    $("#buttontest").click(function(){
        if ($('#code').val() == "1") {   
            $("#JPC1").show("fast"); //Slide Down Effect
            $("#JPC2").hide(); //Slide Down Effect
            $("#incorrect").hide("fast");    //Slide Up Effect
        }else if ($('#code').val() == "2") {   
            $("#JPC1").hide("fast"); //Slide Down Effect
            $("#JPC2").show("fast"); //Slide Down Effect
            $("#incorrect").hide();    //Slide Up Effect
        } 
        else {
            $("#incorrect").show();    //Slide Up Effect
        }
    });
});

This is, in my opinion a cleaner and stackble way to do something like that. 在我看来,这是一种更清洁且可堆叠的方式来执行此类操作。

HTML 的HTML

<p>ENTER YOUR INVITATION CODE</p>
<div id="incorrect" class="ini-hide">Sorry, Try again!</div>
<input type='text' id="code"/>
<input type='button' id="buttontest" value="VERIFY"/>

<div id="JPC1" class="ini-hide">CONTENT 1</div>
<div id="JPC2" class="ini-hide">CONTENT 2</div>

Notice, I create a class for everything that I want to being right from the begin. 注意,我从一开始就为所有想要创建的类创建一个类。

CSS: CSS:

#incorrect {display:none;} #code {color:#000;}
#JPC1, #JPC2, div.ini-hide {display:none;}

And now the JS. 现在是JS。

$(document).ready(function(){
    $("#buttontest").click(function(){
    $("div.ini-hide").hide("fast"); //Hide everything in every buttom click.
    switch($('#code').val()){ //Get value from the input
      case "1": //the value is 1?
            $("#JPC1").show("fast"); //Slide Down Effect and show JPC1
        break;
      case "2": //the value is 2?
            $("#JPC2").show("fast"); //Slide Down Effect and show JPC2
      break;
      default: //No value find to compare?
            $("#incorrect").show("fast");    //Slide Up Effect and show incorrect
      break;
    }
    });
});

Here is the fiddle http://jsfiddle.net/x2tjpzq5/1/ 这是小提琴http://jsfiddle.net/x2tjpzq5/1/

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

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