简体   繁体   English

通过选择Html.DropDownListFor禁用/启用Html.EditorFor

[英]Disable/Enable Html.EditorFor from selection of Html.DropDownListFor

I am trying to enable/disable a set of EditorFor controls depending on the selection made from a Html.DropDownListFor control. 我试图根据从Html.DropDownListFor控件进行的选择来启用/禁用一组EditorFor控件。

My drop down has 4 elements in it and I want to enable a set of editor controls when each element in the drop down is selected. 我的下拉菜单中包含4个元素,并且当下拉列表中的每个元素都被选中时,我想启用一组编辑器控件。

Currently I have the following simple code: 目前,我有以下简单代码:

<div id="label-address-type" class="line">
    @Html.BasicLabelFor(m => m.ReturnsLabelAddressType)
    @Html.DropDownListFor(m => m.ReturnsLabelAddressType, new   SelectList(Model.AddressTypeList(Model.ReturnsLabelAddressType), "Value", "Text"))
</div>
<div class="line">
          @Html.BasicLabelFor(m => m.OtherReturnsLabelAddress.AddressLine1)
    @Html.EditorFor(m => m.OtherReturnsLabelAddress.AddressLine1)
</div>
<div class="line">
    @Html.BasicLabelFor(m => m.OtherReturnsLabelAddress.AddressLine2)
    @Html.EditorFor(m => m.OtherReturnsLabelAddress.AddressLine2)
</div>
<div class="line">
    @Html.BasicLabelFor(m => m.OtherReturnsLabelAddress.AddressLine3)
    @Html.EditorFor(m => m.OtherReturnsLabelAddress.AddressLine3)
</div>
<div class="line">
    @Html.BasicLabelFor(m => m.OtherReturnsLabelAddress.AddressLine4)
    @Html.EditorFor(m => m.OtherReturnsLabelAddress.AddressLine4)
</div>
<div class="line">
    @Html.BasicLabelFor(m => m.OtherReturnsLabelAddress.LocationCode)
    @Html.EditorFor(m => m.OtherReturnsLabelAddress.LocationCode)
</div>

Thanks in advance. 提前致谢。

You can put an id to your dropdown. 您可以在下拉菜单中添加ID。 And the id of every EditorFor could be the same with the text of the dropdown choice. 每个EditorFor的ID都可以与下拉选项的文本相同。 And thenuse jquery to get the dropdown choice. 然后使用jquery获取下拉菜单。 After that you could disable the editor you want 之后,您可以禁用所需的编辑器

var selectedOption = $("#yourdropdownid option:selected").text();
$('#'+selectedOption).prop('disabled', true); 

If you are using bootstrap you can do 如果您正在使用引导程序,则可以执行

$('#'+selectedOption).addClass("disabled");

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

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