简体   繁体   English

使用“ jQuery”选择“选项” Div隐藏/显示

[英]Select “Option” Div Hide/Show using JQuery

I am trying to create an auto hide/show function for when someone selects a certain "option" on a select element. 我正在尝试为某人在选择元素上选择某个“选项”时创建自动隐藏/显示功能。 Here's what I have for the "selector": 这是“选择器”的功能:

<li id="foli16" class="name notranslate">
                    <label class="desc" id="title16" for="Field16">
                        Repeat Pattern
                        <span id="req_16" class="req"></span>
                    </label>
                    <span class="left">
                        <select id="Field16" style="margin-left: 0px;" name="" class="field select">
                            <option value="" <?php echo $doc_selected; ?>>None</option>
                            <option value="" <?php echo $doc_selected; ?>>Daily</option>
                            <option value="" <?php echo $doc_selected; ?>>Weekly</option>
                            <option value="" <?php echo $doc_selected; ?>>Monthly</option>
                            <option value="" <?php echo $doc_selected; ?>>Yearly</option>
                        </select>
                    </span>
                    <p class="instruct" id="instruct16"><small>How often does this test occur?</small></p>
                </li>

When someone selects the "daily" option, I want the div with the id of "daily" to show. 当有人选择“每日”选项时,我希望显示ID为“每日”的div。 If they select a different one, it needs to close the visible one and show the new selection. 如果他们选择另一个,则需要关闭可见的一个并显示新的选择。

I have done this before with click buttons and a single toggle, but I am unsure how to write this for such a large toggle, especially using a select "option". 我之前已经通过单击按钮和单个切换完成了此操作,但是我不确定如何针对如此大的切换(特别是使用选择“选项”)编写此代码。 I assume my options will need ID's as well. 我认为我的选项也将需要ID。

You have 5 option elements, if you have 5 target elements that should be hidden/shown according to the selected option, you can add a class to your target elements and use eq and index methods: 您有5个选项元素,如果有5个目标元素应根据所选选项隐藏/显示,则可以向目标元素添加一个类,并使用eqindex方法:

var $targets = $('.theTargets');

$('#Field16').change(function(){
    var i = $('option:selected', this).index();
    $targets.hide().eq(i).show();
});

In case that you want to select the target element by ID: 如果要通过ID选择目标元素:

$(function(){
    var $targets = $('.theTargets'); // If they have a common class name

    $('#Field16').change(function(){
        var id = this.value.toLowerCase();
        $targets.hide().filter('#' + id).show();
    })
})

You can use the change function for the select field, and grab the ID of the selected option. 您可以将更改功能用于选择字段,并获取所选选项的ID。

<script>
    $(document).ready(function() {

        //This will fire when an option is selected from <select>
        $("#Field16").change(function() {
            //Add a class to all div's so you can hide previous ones
            $(".whateveryourclassis").hide();

            //Get the ID of the selected option
            var selectedID = $(this).attr("id") + "div";

            //Show the right div. (assuming the div you want has the same ID as option
            //with "div" after it
            $("#" + selectedID).show();
        }

    }
</script>

You could do something like this: 您可以执行以下操作:

$('select').on('change', function () {
    $('li').hide()
    $('#' + this.value).show();
});

Building from tymeJV's answer, 根据tymeJV的答案,

I would give each of the divs which will be hidden/shown a class name such as showhide , then 我会给每个将被隐藏/显示的div类一个名称,例如showhide ,然后

$("#Field16").change(function() {
    //Get the ID of the desired div
    var selectedID = $(this).val();

    $(".showhide").hide();
    $("#" + selectedID).show();
}

You can try something like this. 您可以尝试这样。

You can bind the select to an onchange event and grab the value like. 您可以将select绑定到一个onchange事件,并获取类似的值。

$('#Field16').bind('change',function(){
    $('.box').hide(); // All the elements you want to hide on change.
    $('#'+$(this).val()).show();
});

http://jsfiddle.net/XKLx8/ http://jsfiddle.net/XKLx8/

Exactly what you want: 正是您想要的:

HTML HTML

<select id="Field16" style="margin-left: 0px;" name="" class="field select">
                            <option value="">None</option>
                            <option value="">Daily</option>
                            <option value="">Weekly</option>
                            <option value="">Monthly</option>
                            <option value="">Yearly</option>
                        </select>
<div id="none">I am None</div>
<div id="daily">I am Daily</div>
<div id="monthly">I am Monthly</div>
<div id="weekly">I am Weekly</div>
<div id="yearly">I am Yearly</div>

jQuery jQuery的

jQuery(document).ready(function($){
    hide_all();
    function hide_all(){
    $("#Field16 option").each(function(){
        var ids=$(this).text().toLowerCase();
        $('#'+ids).hide();
    });
    }
    $("#Field16").change(function(){
        hide_all();
        var val=$('option:selected',this).text().toLowerCase();
        $('#'+val).show();
    });

});

here is the demo: http://jsfiddle.net/8wKzQ/ 这是演示: http : //jsfiddle.net/8wKzQ/

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

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