Enable an input field according to an Enum

Asked

Viewed 65 times

2

I have an Enum of schooling if in case the user selects the level of education as an incomplete or complete superior I want to appear a field to insert which is the course

How can I do that ?

    <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            @Html.LabelFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EnumDropDownListFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.GrauEscolaridade, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                </div>



   public enum GrauEscolaridade{
        [Display(Name = "Superior incompleto ")]
        SuperiorIncompleto = 15,
        [Display(Name = "Superior Completo")]
        SuperiorCompleto = 16
}
  • you could post part of what you’ve done so far, it would make it easier for us to try to help you.

  • I posted the structure I have

1 answer

2


Make a change to your dropdownListFor: Added "@id = 'name_meu_dropDownList'"

<div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            @Html.LabelFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EnumDropDownListFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "form-control", @id = "nome_do_meu_dropDownList" })
                                @Html.ValidationMessageFor(model => model.GrauEscolaridade, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                </div>

Place at the end of your view (Html)

<input type="text" name="NomeCurso">


// Referência ao framework jquery CDN
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>



<script type="text/javascript">

$(document).ready(function() {

    $('#NomeCurso').hide();

    $("#nome_do_meu_dropDownList").change(function () {

       // 15 e 16 são o Id do Superior completo ou superior incompleto
       if ($("#nome_do_meu_dropDownList").val() == '15' || $("#nome_do_meu_dropDownList").val() == '16') {
           $('#NomeCurso').show(); // Exibe o campo text para digitar o nome do curso
       } else {
           $('#NomeCurso').hide(); // Oculta o campo nome do curso
       }

   });

});

</script>
  • Could you give me an example of how I would use my code ?

  • Updated response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.