Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

jquery - Show div based on select value

I'm trying to make a form that changes after the users makes a choice. If the user select "Option A" then show content of the div with the class "option1".

I have already done this with very simple jquery(see code below or jsfiddle). What I can't figure out is how to make it dynamic.

Here is how I would do it in my head:

  1. Get all option values from "#select" and put into array.
  2. Replace the content of "hideAll" function with the new array.
  3. Make some kind of "for-each-function" that runs though the array and makes the if stament.

A little note: The option value are always the same as the div class.

var hideAll = function() {
  $('.option1, .option2, .option3').hide();
}

$('#select').on('change', function() {
  hideAll();
  var category = $(this).val();
  console.log(category);
  if (category === 'option1') {
    $('.option1').show();
  }
  if (category === 'option2') {
    $('.option2').show();
  }
  if (category === 'option3') {
    $('.option3').show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="post">
  <label for="option">Options</label>
  <select name="select" id="select">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
  </select>

  <div class="option1" style="display:block;">
    <label for="countries">Countries</label>
    <select name="countries">
      <option value="denmark">Denmark</option>
      <option value="norway">Norway</option>
      <option value="uk">UK</option>
    </select>
  </div>

  <div class="option2" style="display:none;">
    <label for="letters">Letters</label>
    <select name="letters">
      <option value="a">A</option>
      <option value="b">B</option>
      <option value="c">C</option>
    </select>
  </div>

  <div class="option3" style="display:none;">
    <label for="numbers">Numbers</label>
    <select name="numbers">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
  </div>
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Give your option divs another class

<form method="post">
<label for="option">Options</label>
<select name="select" id="select">
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
  <option value="option3">Option 3</option>
</select>

<div class="option1 optiondiv" style="display:block;">
<label for="countries">Countries</label>
<select name="countries">
  <option value="denmark">Denmark</option>
  <option value="norway">Norway</option>
  <option value="uk">UK</option>
</select>
 </div>

<div class="option2 optiondiv" style="display:none;">
<label for="letters">Letters</label>
<select name="letters">
   <option value="a">A</option>
   <option value="b">B</option>
   <option value="c">C</option>
</select>
</div>

<div class="option3 optiondiv" style="display:none;">
<label for="numbers">Numbers</label>
<select name="numbers">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
</select>
</div>

Use your new class in hideall

var hideAll = function() {
    $('.optiondiv').hide();
}

Use the select value to display a block

$('#select').on('change', function() {
    hideAll();
    var category = $(this).val();
    $('.' + category).show();
});

http://jsfiddle.net/yd5qsquL/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...