简体   繁体   English

在这些div内部的选择框中动态加载选项后,如何显示/隐藏div元素?

[英]How to show/hide div element after dynamically loading options in select box inside these div's?

I am having 4 dropdown list inside 4 div's and I want to hide and show this div's on dropdown selection from top to botton. 我在4个div内有4个下拉列表,我想隐藏并显示该div在从顶部到波顿的下拉选择中。 If user changes the option in first dropdown then the immediate next div should be shown and the div's following this next div's should fadeOut(). 如果用户在第一个下拉菜单中更改了该选项,则应显示紧邻的下一个div,并且该下一个div之后的div应该淡出()。 [situation- Every next Dropdown is filled with options depending on the option selected in parent Dropdown List dynamically] Now I need the Jquery code as short as possible, hence I tried this.... How to select all div element after some specific number of div elements? [情况-每个下一个Dropdown都会根据父下拉列表中动态选择的选项填充选项]现在,我需要尽可能短的Jquery代码,因此我尝试了此操作。... 如何在某个特定数字后选择所有div元素div元素? and made some changes according to my situation.Here is the code.... 并根据我的情况进行了一些更改。这是代码。

Html: HTML:

<div class="wrapper-inner">
    <div class="OrganisationWrapper onChangeHideWrapper" id="OrganisationWrapper" data-title="1">
        @*data-title is used as index for show/hide on Ddl change*@
        @Html.DropDownListFor(model => model.OrgId, new SelectList(Model.Organisations, "Key", "Value"), "Select Organization..!", new { id = "Organisation", @class = "selectpicker", data_style = " btn-info", data_live_search = "true" })
    </div>
    <div class="SpaceWrapper onChangeHideWrapper" id="SpaceWrapper" data-title="2">
        @Html.DropDownListFor(model => model.SpaceId, new SelectList(string.Empty, "Value", "Text"), "Loading Space..!", new { id = "Space", @class = "selectpicker", data_style = " btn-info", data_live_search = "true" })
   </div>
......
</div>

JQuery: JQuery的:

$("#Organisation").change(function () {
    if ($(this).length > 0) {
        index = $(this).parent(".onChangeHideWrapper").attr("data-title");
        $('.wrapper - inner .onChangeHideWrapper:gt(' + index + ')').fadeOut();
    }
    else{
        $('.onChangeHideWrapper').fadeOut();
    }      
}
$("#Space").change(function () {
    if ($(this).length > 0) {
        index = $(this).parent(".onChangeHideWrapper").attr("data-title");
        $('.wrapper - inner .onChangeHideWrapper:gt(' + index + ')').fadeOut();
    }
    else{
        $('.onChangeHideWrapper').fadeOut();
    }
}

But fadeOut() is not working. 但是fadeOut()无法正常工作。 Can anyone help me on this ? 谁可以帮我这个事 ?

Thanx in advance 提前感谢

You can simplify this quite a bit if you use operations relative to the changed select. 如果使用相对于更改后的选择的操作,则可以将其简化很多。

  • You need to initially hide all but the first selector. 您首先需要隐藏除第一个选择器以外的所有内容。
  • When a select changes, see if the val() is the empty string. 当选择更改时,请查看val()是否为空字符串。
  • If a selection is made fade in the next div only (using closest('div').next() ) 如果做出选择,则仅在下一个div中淡入(使用closest('div').next()
  • If it is blank, fadeout out all subsequent divs using nextAll() 如果为空,则使用nextAll()淡出所有后续div

eg 例如

 $(document).ready(function () {
    $('.onChangeHideWrapper:gt(0)').hide();
    $('.selectpicker').change(function () {
        if ($(this).val() != "") {
            $(this).closest('.onChangeHideWrapper').next().fadeIn();
        } else {
            $(this).closest('.onChangeHideWrapper').nextAll().fadeOut();
        }
    });
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/bar95jzq/8/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/bar95jzq/8/

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

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