在另一个下拉列表中选择特定选项时删除DropDownListFor选择

时间:2015-08-25 14:16:30

标签: javascript jquery asp.net-mvc

显然我对理解JS有一个问题。我是新手,我试图让它适用于以下用例:

  

如果从“纪律”下拉列表中选择“医疗保健”,则在其他下拉列表中将不会显示“私人部门”选项。

<div class="col-md-6 jurisdictionParentDiv">
    <div class="form-group">
        @Html.LabelFor(m => m.Jurisdiction, new { @class ="control-label" })
        @Html.DropDownListFor(
            m => m.Jurisdiction,
            GlobalLists.JurisdictionList
                .Where(k => k.Key != "Military")
                .Select(
                    k => new SelectListItem {
                        Text = k.Value,
                        Value = k.Key,
                        Selected = k.Value == Model.Jurisdiction
                    }),
            "-- Select --",
            new { @class = "form-control input-sm" }
        )
    </div>
</div> 

<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            @Html.LabelFor(m => m.Discipline, new { @class = "control-label" })
            @Html.DropDownListFor(
                m => m.Discipline,
                GlobalLists.DisciplineList
                    .Select(
                        k => new SelectListItem {
                            Text = k.Value,
                            Value = k.Key,
                            Selected = k.Value == Model.Discipline
                        }),
                "-- Select --",
                new { @class = "form-control input-sm" }
            )
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#Jurisdiction').change(function () {
            var el = $('#Discipline');
            if (el.val() === 'Healthcare') {
                $('#Jurisdiction').remove('Private Sector')
            }
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

你可以这样做,

if ($('#Discipline').val() === "Healthcare") {
    $("#Jurisdiction option[value='Private Sector']").hide();
}