【问题标题】:Use JQuery to hide and show a div based on drop down selection使用 JQuery 根据下拉选择隐藏和显示 div
【发布时间】:2017-12-30 09:14:23
【问题描述】:

我正在尝试隐藏下拉框并显示一组特定的 div(其中类和 id 相同)。

下拉列表的值对应div的名称

我的尝试:

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
  $(function () {
        $("#submissionType").change(function () {
          var submission = $(this).val();
            if ($(this).val()) {
                $("#"+submission).show();
            } else {
                $("#"+submission).hide();
            }
        });
    });
  </script>

html:

<select id="submissionType">
              <option>Choose Submission Type</option>
              <option value="member-submission">member-submission</option>
              <option value="researcher-submission">researcher-submission</option>
              <option value="student-submission">student-submission</option>
              <option value="paper-submission">paper-submission</option>
          </select>

<div class ="new-member-background">
  <div class ="member-submission" id="member-submission">
       <form  action="SubmitData.php" method="post">
            <input type="text" name="first" placeholder="First Name" />
              <br/>
            <input type="text" name="last" placeholder="Last Name" />
              <br/>
            <input type="text" name="title" placeholder="Title" />
              <br/>
            <input type="text" name="institution" placeholder="Institution" />
              <br/>
            <input type="number" name="dept_code" placeholder="Department Code" />
              <br/>
            <input type="text" name="division" placeholder="Division" />
              <br />
            <input type="text" name="email" placeholder="Email" />
              <br/>
            <input type="text" name="website" placeholder="Website" />
              <br/>
        <button type="submit" name="submit">Sign Up</button>
      </form>
</div>
/**...Other divs...*/
</div>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您应该将它们全部隐藏,然后显示选定的一个。例如:

    $(function () {
        //hide them all to begin with. It would be easier if they all shared a classname. You could also do this in CSS)
        $("#member-submission, #researcher-submission, #student-submission, #paper-submission").hide();
        //on change
            $("#submissionType").change(function () {
              var submission = $(this).val();
              //hide them all:
              $("#member-submission, #researcher-submission, #student-submission, #paper-submission").hide();
              //show the selected one
              $("#"+submission).show();
            });
        });
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    
    <select id="submissionType">
                  <option>Choose Submission Type</option>
                  <option value="member-submission">member-submission</option>
                  <option value="researcher-submission">researcher-submission</option>
                  <option value="student-submission">student-submission</option>
                  <option value="paper-submission">paper-submission</option>
              </select>
    
    <div class ="new-member-background">
      <div class ="member-submission" id="member-submission">
           <form  action="SubmitData.php" method="post">
                <input type="text" name="first" placeholder="First Name" />
                  <br/>
                <input type="text" name="last" placeholder="Last Name" />
                  <br/>
                <input type="text" name="title" placeholder="Title" />
                  <br/>
                <input type="text" name="institution" placeholder="Institution" />
                  <br/>
                <input type="number" name="dept_code" placeholder="Department Code" />
                  <br/>
                <input type="text" name="division" placeholder="Division" />
                  <br />
                <input type="text" name="email" placeholder="Email" />
                  <br/>
                <input type="text" name="website" placeholder="Website" />
                  <br/>
            <button type="submit" name="submit">Sign Up</button>
          </form>
    </div>
    /**...Other divs...*/
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多