简体   繁体   中英

Dropdown List with content for each option (<select>,<option>)

I just try to make a Dropdown List. When I'm clicking at one option, I want, that for each option a specific content (text) appears. But I have no idea, how I can do this. Here's the code:

<html>
  <body>
    <form name="dropdown-list">
      <select>
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
    </form>
  </body>
</html>

use jquery.. assign ID to your select element and use below code

$('#select-id').change(function() {
    //piece of code to do when option changes
})

我认为您需要使用Javascript。

I think this will do the trick. We are gonna hide all other blocks that we don't need and will appear the only one that we need

<html>
    <head>
      <style>
        .yourContentHere {
            display: none;
        }
        .activeContent {
           display: block;
        }
      </style>
      <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    </head>
    <body>

     <form name="dropdown-list">

      <select>

       <option value="1" selected="selected">1</option>

       <option value="2">2</option>

       <option value="3">3</option>

      </select>

     </form>

    <div id="yourDropdownChoiceContent">
      <div class="yourContentHere activeContent">This wiil appear if we click dropdown 1</div>
      <div class="yourContentHere">This wiil appear if we click dropdown 2</div>
      <div class="yourContentHere">This wiil appear if we click dropdown 3</div>
    </div>
    <script>
    $('select').change(function(){
        var chosenOption = $('select option:selected').index()
        $('.yourContentHere').removeClass('activeContent')
        $('.yourContentHere').eq(chosenOption).addClass('activeContent')
    });
    </script>
 </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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