【问题标题】:showing one of 3 divs depending on which select is chosen [duplicate]根据选择的选择显示 3 个 div 之一[重复]
【发布时间】:2014-01-11 22:09:58
【问题描述】:

我有一个选择框,其中包含 3 个选项主体、强度、距离。我想使用 javascript / jquery 来显示一个 div,具体取决于选择了哪些选项。到目前为止,我刚刚完成了按钮推送http://codepen.io/Irish1/pen/iwKam

html

<form>
  <p>
    <label for="select">
      Goal Type
    </label>
    <select class="typeselect">
      <option value=""></option>
      <option value="body">
        Body
      </option>
      <option value="strength">
        Strength
      </option>
      <option value="distance">
        Distance
      </option>
    </select>
    <button class="toggle">push</button>
  </p>
  <div class="body">
    <p>
      <label for="body">
       Body
      </label>
    </p>
  </div>
  <div class="strength">
    <p>
      <label for="strength">
        Strength
      </label>
    </p>
  </div>
  <div class="distance">
    <p>
      <label for="distance">
       Distance
      </label>
    </p>
  </div>


</form>

css

.body {
  display: none;
}

.strength {
  display: none;
}

.distance {
  display: none;
}

javascript

$('.toggle').click(function(){
  $('.body').toggle();
});

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    试试

    //dom ready handler
    jQuery(function () {
        //all the elements that has to be shown/hidden, it is cached for easy access later
        var $targets = $('.body, .strength, .distance');
    
        //chang handler for the select element
        $('.typeselect').change(function () {
            //hide previously displayed elements
            $targets.hide()
    
            //find the element with the selected option's value as class, if empty option is selected set it to an empty set
            if (this.value) {
                $('.' + this.value).show()
            }
        })
    })
    

    演示:Fiddle

    如果你想让按钮处理程序工作,那么

    //dom ready handler
    jQuery(function () {
        //all the elements that has to be shown/hidden, it is cached for easy access later
        var $targets = $('.body, .strength, .distance');
    
        var $select = $('.typeselect');
    
        //click handler for the button
        $('.toggle').click(function (e) {
            //prevent the default form action of the button click
            e.preventDefault();
    
            //hide previously displayed elements
            $targets.hide()
    
            //selected value
            var val = $select.val();
            //find the element with the selected option's value as class, if empty option is selected set it to an empty set
            if (val) {
                $('.' + val).show()
            }
        })
    })
    

    演示:Fiddle

    【讨论】:

    • Arun 的做法与我为您整理的内容类似:codepen.io/anon/pen/ktesh
    • @michaelt 对不起...你是什么意思
    • 我 fork @Irish 的原始代码,做了一些小的修改,并有一个工作示例:codepen.io/anon/pen/ktesh 只有几行 jQuery 和对 dom 的真正小修改。
    • @michaelt 唯一的问题是,当您选择空选项时,它会引发错误......也可以处理它就可以了......但我建议你缓存选择器的值('.target')
    • 我什至甚至完全删除了空的
    【解决方案2】:
    $('#typeselect').change(function(event) {
       $('.target').hide();
       var option = $(this).val();
       if (option != "") $('.'+option).show();
    });
    

    以及对 html 的一些小修改

    <form>
      <p>
        <label for="select">Goal Type</label>
        <select id="typeselect" class="typeselect">
          <option value=""></option>
          <option value="body">Body</option>
          <option value="strength">Strength</option>
          <option value="distance">Distance</option>
        </select>
      </p>
      <div class="body target">
        <p><label for="body">Body</label></p>
      </div>
      <div class="strength target">
        <p><label for="strength">Strength</label></p>
      </div>
      <div class="distance target">
        <p><label for="distance">Distance</label></p>
      </div> 
    </form>
    

    您可以在 codepen 上查看和编辑:http://codepen.io/anon/pen/ktesh

    【讨论】:

    • 你能在我的 cmets 中进行更正吗
    • 那会是什么更正?
    • 我假设您的意思是空白选项标签。我添加了一个条件来测试来自选项的值。如果它不为空,它将显示相应的
    【解决方案3】:

    为 Select 和 Div 指定名称/ID。

    var x = document.getElementById("SelectId / Name").selected;
    
        if(x==0) {
            document.getElementById("body").style.display = "block";
        } else {
            \\ Similarly for other div to display on the basis of x
        }
    

    【讨论】:

      【解决方案4】:

      试试这个:

      $('.typeselect').change(function(){
         $('div').hide();
         if($(this).val() == ""){
            $(this).children('option:first-child').show();
         }
         else
            $('.' + $(this).val()).show();
      });
      

      【讨论】:

      • 不是$(this).value 也不是$(this).val()this.value
      • 对不起!我对 javascript 感到困惑
      • 如果选择了空选项,它会再次抛出错误
      • 我认为选项(在这种情况下为“”)是默认选择的。所以我会为此修改。
      猜你喜欢
      • 1970-01-01
      • 2013-10-10
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多