隐藏表单的问题不会重新隐藏自己

时间:2016-04-04 14:56:42

标签: php html css

所以我使用CSS根据单击的单选按钮显示不同的文本框。它主要是工作,但我会列出我的代码下方底部的问题。

CSS:

.reveal-if-active {


opacity: 0;
  max-height: 0;
  overflow: hidden;
  font-size: 16px;
  -webkit-transform: scale(0.8);
          transform: scale(0.8);
  -webkit-transition: 0.5s;
  transition: 0.5s;
}
.reveal-if-active label {
  display: block;
  margin: 0 0 3px 0;
}
.reveal-if-active input[type=text] {
  width: 50%;
}
input[type="radio"]:checked ~ .reveal-if-active, input[type="checkbox"]:checked ~ .reveal-if-active {
  opacity: 1;
  max-height: 100px;
  padding: 10px 20px;
  -webkit-transform: scale(1);
          transform: scale(1);
  overflow: visible;
}

形式:

        <h1>What group does this person belong to?</h1>
    <br />
    <form action="uploaderPerson.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?php echo $_GET["id"]; ?>"/>
        <input type="hidden" name="action" value="2"/>

        <input type="radio" name="option" value="Employer" id="Employer"> Employer<br>
        <div class="reveal-if-active">
        <label for="empname">Employer's Name: </label>
        <input type="text" id="empname" name="empname" class="require-if-active" data-require-pair="#Employer" placeholder="empname">
        <label for="email">Employer's Email: </label>   
        <input type="text" id="email" name="email" class="require-if-active" data-require-pair="#Employer" placeholder="email">
        <label for="phone">Employer's Phone: </label>
        <input type="text" id="phone" name="phone" class="require-if-active" data-require-pair="#Employer" placeholder="phone">
        </div>
        <br />

        <input type="radio" name="option" value="Faculty"> Faculty<br>
        <div class="reveal-if-active">
        <label for="hiredate">Hire Date: </label>
        <input type="text" id="hiredate" name="hiredate" class="require-if-active" data-require-pair="#Faculty" placeholder="hiredate">
        <label for="isretired">Retired?: </label>   
        <input type="text" id="isretired" name="isretired" class="require-if-active" data-require-pair="#Faculty" placeholder="isretired">
        <label for="degreeinstitute">Degree Institute: </label>
        <input type="text" id="degreeinstitute" name="degreeinstitute" class="require-if-active" data-require-pair="#Faculty" placeholder="degreeinstitute">
        </div>
        <br />

        <input type="radio" name="option" value="Student"> Student<br>
        <div class="reveal-if-active">
        <label for="graduation">Hire Date: </label>
        <input type="date" id="graduation" name="graduation" class="require-if-active" data-require-pair="#Student" placeholder="mm/dd/yyyy">
        </div>      


        <br /><br />
        <input type="submit" value="Submit">
    </form>

我的问题是,当我点击一个单选按钮时,它们都会出现。然后我点击Faculty按钮,Employer一个消失,但学生一个保持活跃状态​​。然后我点击学生一,学院一个终于消失了。这不是一个大问题,但我无法弄清楚我错过了什么。我想象它简单,但我似乎无法在任何地方找到它。我知道有几个人使用javascript来帮助解决这类问题,但我使用它并没有太多运气并且大部分都是这样做的。任何输入都表示赞赏。

2 个答案:

答案 0 :(得分:1)

您使用的是错误的CSS选择器。

只要你严格遵守一些规则,就可以用纯CSS做到这一点:

a)您需要使用+选择器才能选择直接兄弟。 b)您需要确保.reveal-if-active是您控制它的复选框的直接兄弟。

CSS并不是要完成这些任务,我强烈建议不要使用CSS来实现这些结果,因为它严重依赖于DOM文档中的细节,您可能需要更改这些细节以实现其他样式要求。但是,如果它需要是纯CSS,您可以执行以下操作:

<强> CSS

.reveal-if-active {


opacity: 0;
  max-height: 0;
  overflow: hidden;
  font-size: 16px;
  -webkit-transform: scale(0.8);
          transform: scale(0.8);
  -webkit-transition: 0.5s;
  transition: 0.5s;
}
.reveal-if-active label {
  display: block;
  margin: 0 0 3px 0;
}
.reveal-if-active input[type=text] {
  width: 50%;
}
input[type="radio"]:checked + .reveal-if-active, input[type="checkbox"]:checked + .reveal-if-active {
  opacity: 1;
  max-height: 100px;
  padding: 10px 20px;
  -webkit-transform: scale(1);
          transform: scale(1);
  overflow: visible;
}

<强> HTML

<form action="uploaderPerson.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="id" value="id"/>
    <input type="hidden" name="action" value="2"/>

    <input type="radio" name="option" value="Employer" id="Employer"> Employer
    <div class="reveal-if-active">
    <label for="empname">Employer's Name: </label>
    <input type="text" id="empname" name="empname" class="require-if-active" data-require-pair="#Employer" placeholder="empname">
    <label for="email">Employer's Email: </label>   
    <input type="text" id="email" name="email" class="require-if-active" data-require-pair="#Employer" placeholder="email">
    <label for="phone">Employer's Phone: </label>
    <input type="text" id="phone" name="phone" class="require-if-active" data-require-pair="#Employer" placeholder="phone">
    </div>
    <br />

    <input type="radio" name="option" value="Faculty"> Faculty
    <div class="reveal-if-active">
    <label for="hiredate">Hire Date: </label>
    <input type="text" id="hiredate" name="hiredate" class="require-if-active" data-require-pair="#Faculty" placeholder="hiredate">
    <label for="isretired">Retired?: </label>   
    <input type="text" id="isretired" name="isretired" class="require-if-active" data-require-pair="#Faculty" placeholder="isretired">
    <label for="degreeinstitute">Degree Institute: </label>
    <input type="text" id="degreeinstitute" name="degreeinstitute" class="require-if-active" data-require-pair="#Faculty" placeholder="degreeinstitute">
    </div>
    <br />

    <input type="radio" name="option" value="Student"> Student
    <div class="reveal-if-active">
    <label for="graduation">Hire Date: </label>
    <input type="date" id="graduation" name="graduation" class="require-if-active" data-require-pair="#Student" placeholder="mm/dd/yyyy">
    </div>      


    <br /><br />
    <input type="submit" value="Submit">
</form>

示例jsFiddle

答案 1 :(得分:0)

我想你会想要这个example。它显示了如何使用javascript来隐藏和显示你的复选框,你可能还需要一些ifs

编辑:要使用它,您将要提供javascript / JQuery可以抓取的复选框ID