用Jquery显示和隐藏select中的元素

时间:2013-06-26 03:50:28

标签: jquery html

我正在尝试根据其值=“”在选择下拉菜单中显示和隐藏div。

有人可以向我解释为什么这不起作用吗?这是Jfiddle:http://jsfiddle.net/JNyce/11/

$('.section_container').children().hide();
$('#form_selection').change(function() {


  var selection = $(this).parent().find("option:selected").text();

    var newselection = selection.split(' ').join('_');

    $('.section_container'+newselection).show();

    });

HTML

<div class="box" style="border: 1px solid black; margin-top:100px;">
<select id="form_selection">
<option value="Program 1">Program 1</option>
<option value="Program 1">Program 2</option>
</select>

        <div class="section_container">
            <div class="Program_1" style="height:200px; border:1px solid black"> 
            <div class="title">Testing</div>
            <span> orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing</span>
            <div class="section_logo" style="width:100px; height:100px; float:left;"><img src="http://www.dublin.k12.ca.us/cms/lib5/CA01001424/Centricity/Domain/35/homeschool.jpg"> </div><button class="blue-pill" style="float:right;">Select Program</button>
            </div>

            <div class="Program_2" style="height:200px; border:1px solid black"> 
            <div class="title">Testing</div>
            <span>orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing </span>
            <div class="section_logo" style="width:100px; height:100px; float:left;"><img src="http://www.dublin.k12.ca.us/cms/lib5/CA01001424/Centricity/Domain/35/homeschool.jpg"> </div>
             <button class="blue-pill" style="float:right;">Select Program</button>
            </div>
     </div>

2 个答案:

答案 0 :(得分:3)

首先你有value="Program 1"两次:

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

这应该是:

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

其次,您没有正确选择班级:

$('.section_container' + newselection).show();

这应该是:

$('.section_container .' + newselection).show();

<强> Demo

<强>的Javascript

$('.section_container > div').hide();
$('#form_selection').change(function () {

    var selection = $(this).val();
    var newselection = selection.replace(' ', '_');

    $('.section_container > div').hide();
    $('.section_container .' + newselection).show();

});

答案 1 :(得分:1)

这是 FIDDLE

$('.section_container').children().hide();
$('#form_selection').change(function () {


    var selection = $(this).parent().find("option:selected").text();

    var newselection = selection.split(' ').join('_');
    console.log(newselection);

    $('.section_container').children().hide();
    $('.section_container').find('.' + newselection).show();
});