简体   繁体   English

如何基于下拉选择显示div

[英]How to display a div based on a drop down selection

I've searched for a specific solution but cant seem to find the answer I'm looking for. 我已经在寻找一种特定的解决方案,但似乎找不到我要找的答案。 I'm developing a CMS system that selects a pre-developed module for each section. 我正在开发一个CMS系统,该系统为每个部分选择一个预先开发的模块。

When a user selects a module from the first dropdown list (select id='section1'), a list of properties will display for this module. 当用户从第一个下拉列表中选择一个模块(选择id ='section1')时,将显示该模块的属性列表。 When a user selects a module from the second dropdown list (select id='section2') the properties of that module should display. 当用户从第二个下拉列表中选择一个模块(选择id ='section2')时,该模块的属性应显示。 There are 5 dropdown lists to choose from, each with the same options. 有5个下拉列表可供选择,每个都有相同的选项。 The problem I'm having is that when the first selection is made, the properties display all good and well. 我遇到的问题是,当做出第一个选择时,属性显示的很好。 But when the second drop down list is selected, the first selection's set of properties are replaced by the corresponding div of the second selection. 但是,当选择第二个下拉列表时,第一个选择的属性集将被第二个选择的相应div替换。

What I need is for the div of first selection to display even after the second selection is chosen. 我需要的是即使在选择了第二个选择之后也要显示第一个选择的div。 So the properties for both modules should display. 因此,两个模块的属性都应显示。 When the module in the first option is changed, then so should the div displaying it's properties. 当第一个选项中的模块被更改时,显示其属性的div也应更改。 Please help. 请帮忙。 I know this is done with some jquery but I dont know how to manipulate the code to do what I need 我知道这是通过一些jquery完成的,但是我不知道如何操纵代码来完成我需要的工作

Example: 例:

<select id="section1"><option>Please Select</option>
<option value="1">Module 1</option>
<option value="2">Module 2</option>
<option value="3">Module 3</option></select>

<select id="section2"><option>Please Select</option>
<option value="1">Module 1</option>
<option value="2">Module 2</option>
<option value="3">Module 3</option></select>

<div id="module1" class="page" style="display:none;">Module 1 Properties</div>
<div id="module2" class="page" style="display:none;">Module 2 Properties</div>
<div id="module3" class="page" style="display:none;">Module 3 Properties</div>

The pattern repeats for each section, Like I mentioned, there are 5 sections, but I'll get the idea if you could help me with 2 sections. 就像我提到的那样,该模式在每个部分重复,共有5个部分,但是如果您可以在2个部分中为我提供帮助,我就会明白。

The jquery that I'm using is: 我正在使用的jQuery是:

$(function() {
$('#section1').change(function() {
    var val = $(this).val();
    if (val) {
        $('div:not(#module' + val + ')').slideUp();
        $('#module' + val).slideDown();
    } else {
        $('div').slideDown();
    }
});
});

$(function() {
$('#section2').change(function() {
    var val = $(this).val();
    if (val) {
        $('div:not(#module' + val + ')').slideUp();
        $('#module' + val).slideDown();
    } else {
        $('div').slideDown();
    }
});
});

The code 编码

$('div:not(#module' + val + ')').slideUp();

will hide all divs that isn't connected to the dropdown that is selected. 将隐藏所有未连接到所选下拉列表的div。 This will cause the "Module 1"-div to hide when you are selecting in the "Module 2"-dropdown. 在“模块2”(Module 2)下拉列表中进行选择时,这将导致“模块1” -div隐藏。

Try and remove that, I can't test it for the moment. 尝试删除它,我暂时无法对其进行测试。

http://jsfiddle.net/hc6sh/1/ http://jsfiddle.net/hc6sh/1/

$('select option').click(function(){
    var num = $(this).index()
    var num =  '#module'+num
 // alert(num)
    $('.page').slideUp();
     $(num).slideDown();
});

Try this one 试试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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