使用javascript下拉列表

时间:2015-04-03 18:49:35

标签: javascript html drop-down-menu

我有这3个下拉列表。我想根据第一个DDL中选择的值仅显示1个下拉列表。有人可以帮我这个:

HTML:

<div class="field">
                    <label for="message_for">Message For</label>
                    <select id="message_for" name="message_for"         title="Message For" >
                      <option value='shop'>Shops</option>
                      <option value='offers'>Offers</option>
                      <option value='events'>Events</option>
                      <option value='consultancy'>Consultancy</option>
                    </select>
                </div>

                <div class="field1">
                    <label for="message_type">Message Type</label>
                    <select id="offer_type" name="offer_type" title="Offer     Type" >
                      <option value='special'>Special</option>
                      <option value='recommend'>Recommended</option>
                      <option value='day'>Offer of the Day</option>
                      <option value='hot'>Hot Offer</option>
                    </select>
                </div>

                <div class="field2">
                    <label for="message_type">Message Type</label>
                    <select id="shop_type" name="shop_type" title="Shop Type" >
                      <option value='mall'>Mall</option>
                      <option value='warehouse'>Warehouse</option>
                      <option value='food'>Food</option>
                      <option value='banquet'>Banquet</option>
                      <option value='service'>Service</option>
                      <option value='cosmetics'>Cosmetics</option>
                      <option value='fashion'>Fashion</option>
                    </select>
                </div>

                <div class="field3">
                    <label for="message_type">Message Type</label>
                    <select id="event_type" name="event_type" title="Event Type" >
                      <option value='social'>Social</option>
                      <option value='other'>Other</option>
                    </select>
                </div>

表示如果我在第一个DDL 中选择商店,则第二个DDL应为DIV field2

2 个答案:

答案 0 :(得分:0)

基本上,我们的想法是使用jquery获取message_for select的值,并根据其值显示或隐藏其他选择。

$("#message_for").on("change", function(){
    if($(this).val() == "shop"){
        $(".field1").show();
    }
    //here add more cases to show relevant selects
});

我为你创造了这个jsfiddle - https://jsfiddle.net/guranjanpsingh/h9prdxyy/2/

答案 1 :(得分:0)

最简单的方法是向div添加与option值相同的类,以便定位它们。

&#13;
&#13;
$('div:not(".general")').hide();
$('#message_for').on('change', function() {
  $('div:not(".general")').hide();
  $('.' + $(this).val()).show();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="general">
  <label for="message_for">Message For</label>
  <select id="message_for" name="message_for" title="Message For">
    <option value=''>Please Select</option>
    <option value='shop'>Shops</option>
    <option value='offers'>Offers</option>
    <option value='events'>Events</option>
    <option value='consultancy'>Consultancy</option>
  </select>
</div>

<div class="shop">
  <label for="message_type">Message Type</label>
  <select id="offer_type" name="offer_type" title="Offer     Type">
    <option value='special'>Special</option>
    <option value='recommend'>Recommended</option>
    <option value='day'>Offer of the Day</option>
    <option value='hot'>Hot Offer</option>
  </select>
</div>

<div class="offers">
  <label for="message_type">Message Type</label>
  <select id="shop_type" name="shop_type" title="Shop Type">
    <option value='mall'>Mall</option>
    <option value='warehouse'>Warehouse</option>
    <option value='food'>Food</option>
    <option value='banquet'>Banquet</option>
    <option value='service'>Service</option>
    <option value='cosmetics'>Cosmetics</option>
    <option value='fashion'>Fashion</option>
  </select>
</div>

<div class="events">
  <label for="message_type">Message Type</label>
  <select id="event_type" name="event_type" title="Event Type">
    <option value='social'>Social</option>
    <option value='other'>Other</option>
  </select>
</div>
&#13;
&#13;
&#13;