0

我正在尝试根据它们的 value="" 在选择下拉列表中显示和隐藏 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>
4

2 回答 2

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();

演示

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();

});
于 2013-06-26T03:53:45.083 回答
1

这是小提琴

$('.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();
});
于 2013-06-26T03:59:18.833 回答