繁体   English   中英

如何使用选项 jQuery 隐藏 select2

[英]How to hide select2 with options jQuery

我有带有选项的 select 表单,用于按关键字搜索。 我正在使用select2 lib 但是我的select 表单是动态创建的,我想根据用户的选择隐藏和显示一些 select 表单。 现在的问题是,如果我从列表中选择一些东西:

在此处输入图像描述

对于前生产工厂,它将出现新的select 表格,我可以在其中选择。 (效果很好)

如果我再次 go 到生产资源并选择另一种类型:

在此处输入图像描述

现在我选择了Production 段,prev select 表格是隐藏的,但是如果我想 go 回到Production 工厂,它不会出现:

在此处输入图像描述

我的代码:

$("#productionResources").change(function () {
    var prodRes = $("#productionResources :selected").html();
   $("#selectedProdRes").html(prodRes);
    var id = $(this).val();
    if (id != "") {
        $('#' + id).select2().next().show();
    } 
    idChanging(id);

});

function idChanging(id) {
    $("#productionResources").change(function () {
        $('#' + id).select2().next().hide();
    });
}

HTML:

<div class="display-in-row">
        <label>Production resources</label><br>
        <select class="form-control" id="productionResources">
            <option selected></option>
            <option value="productionPlant">Production plant</option>
            <option value="productionSegment">Production segment</option>
            <option value="valueStream">Value stream</option>
            <option value="workCenterGroup">Work center group</option>
            <option value="workCenter">Work center</option>
            <option value="station">Station"</option>
        </select>
    </div>
    <div class="display-in-row">
        <div class="col-12">
            <label id="selectedProdRes"></label><br>
            <select asp-for="ProductionPlantId" asp-items="Model.ProductionPlant" class="form-control content" id="productionPlant"></select>
            <select asp-for="ProductionSegmentId" asp-items="Model.ProductionSegment" class="form-control content" id="productionSegment"></select>
            <select asp-for="ValueStreamId" asp-items="Model.ValueStream" class="form-control content" id="valueStream"></select>
            <select asp-for="WorkCenterGroupId" asp-items="Model.WorkCenterGroup" class="form-control content" id="workCenterGroup"></select>
            <select asp-for="WorkCenterId" asp-items="Model.WorkCenter" class="form-control content" id="workCenter"></select>
            <select asp-for="StationId" asp-items="Model.Station" class="form-control content" id="station"></select>
        </div>
    </div>

问题来自您的 function idChanging()

一旦您更改第一个 select, idChanging()事件就会设置并保留。 所以每次你 select #productionResources时, idChanging()定义的所有事件都会被触发,所以之前选择的所有选择都将被隐藏。

以下应该有效:

我在所有辅助选择中添加了一个 class secondary-select选择以方便,但这不是必需的:

<div class="display-in-row">
        <div class="col-12">
            <label id="selectedProdRes"></label><br>
            <select asp-for="ProductionPlantId" asp-items="Model.ProductionPlant" class="form-control content secondary-select" id="productionPlant"></select>
            <select asp-for="ProductionSegmentId" asp-items="Model.ProductionSegment" class="form-control content secondary-select" id="productionSegment"></select>
            <select asp-for="ValueStreamId" asp-items="Model.ValueStream" class="form-control content secondary-select" id="valueStream"></select>
            <select asp-for="WorkCenterGroupId" asp-items="Model.WorkCenterGroup" class="form-control content secondary-select" id="workCenterGroup"></select>
            <select asp-for="WorkCenterId" asp-items="Model.WorkCenter" class="form-control content secondary-select" id="workCenter"></select>
            <select asp-for="StationId" asp-items="Model.Station" class="form-control content secondary-select" id="station"></select>
        </div>
    </div>
$("#productionResources").change(function () {
    // Define title
    var prodRes = $("#productionResources :selected").html();
    $("#selectedProdRes").html(prodRes);
    // Make sure none of the 2nd select is visible
    $('.secondary-select').select2().next().hide();
    // Display the select according to the ID
    var id = $(this).val();
    if (id !== '') {
        $('#' + id).select2().next().show();
    } 
});

暂无
暂无

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

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