如何使用选项jQuery隐藏select2

我有带选项的选择表格,可以通过关键字进行搜索。我正在使用 select2 lib 。 但是我的选择表单是动态创建的,我想根据用户的选择隐藏和显示一些选择表单。 现在的问题是,如果我从列表中选择某项:

如何使用选项jQuery隐藏select2

对于前生产工厂,它将显示新的选择表单,我可以在其中进行选择。 (效果很好)

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

如何使用选项jQuery隐藏select2

现在,我选择了生产部门,并隐藏了选择表单,但是如果我想回到生产工厂,它不会出现:

如何使用选项jQuery隐藏select2

我的代码:

$("#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>
skiiyxia 回答:如何使用选项jQuery隐藏select2

问题来自您的函数idChanging()

更改第一选择后,idChanging()事件即被设置并保留。因此,每次选择#productionResources时,都会触发idChanging()定义的所有事件,因此所有先前选择的选择都将被隐藏。

以下方法应该起作用:

我为所有辅助选择添加了一个类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();
    } 
});
本文链接:https://www.f2er.com/3159903.html

大家都在问