Search code examples
javascriptjqueryasp.net-mvchideshow-hide

Hide/Show div on select option change not working


I'm trying to hide/show my div's against the select with id "OpcoesCampos" but this code is not working. Can someone explain my why and give me some help? BTW, is that possible to call a controller method against the select values on selects/inputs?

<body class="img-main" style="background-image: url(https://images.pexels.com/photos/34578/pexels-photo.jpg?cs=srgb&dl=blogging-business-coding-34578.jpg&fm=jpg); background-size: cover;">
    <h2 style="color:white;"> Lista de Estágios/Projetos </h2>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <select class="form-control" id="OpcoesCampos">
                <option>Selecione o filtro</option>
                <option>Propostas Ativas</option>
                <option>Localização</option>
                <option>Ano/Semestre</option>
            </select>
            <input id="Localização" type="text">
            <div class="form-group" id="Ano">
                <div class="col-md-10">
                      // Some options inside here
                </div>
            </div>
            <div class="form-group" id="Semestre">
                <div class="col-md-10">
                      // Some options inside here
                </div>
            </div>
        </div>
        <div class="panel-body">
            // Just a table with content inside here
            <p>
                @Html.ActionLink("Adicionar Projeto/Estágio", "Create")
            </p>
        </div>
    </div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $('#Localização').hide();
        $('#Ano').hide();
        $('#Semestre').hide();
                $(function () {
                    $('#OpcoesCampos').change(function () {
                        e.preventDefault()
                        MostraDropDownList($(this).val());
                    });
                });

        function MostraDropDownList(this) {
            if (myFormType == 'Propostas Ativas') {
                $('#Localização').hide();
                $('#Ano').hide();
                $('#Semestre').hide();
                e.stopPropagation();
            }
            else if (myFormType == 'Localização') {
                $('#Localização').show();
                $('#Ano').hide();
                $('#Semestre').hide();
                e.stopPropagation();
            }
            else if (myFormType == "Ano/Semestre") {
                $('#Localização').hide();
                $('#Ano').show();
                $('#Semestre').show();
                e.stopPropagation();
            }
        }
    </script>
}
 </body>

Solution

  • Here is the Solution.

    $('#Localização').hide();
        $('#Ano').hide();
        $('#Semestre').hide();
                $(function () {
                    $('#OpcoesCampos').change(function (e) {                        
                      MostraDropDownList($(this).val(),e);
                      e.preventDefault();
                    });
                });
    
        function MostraDropDownList(myFormType,e) {
            if (myFormType == 'Propostas Ativas') {
                $('#Localização').hide();
                $('#Ano').hide();
                $('#Semestre').hide();
                e.stopPropagation();
            }
            else if (myFormType == 'Localização') {
                $('#Localização').show();
                $('#Ano').hide();
                $('#Semestre').hide();
                e.stopPropagation();
            }
            else if (myFormType == "Ano/Semestre") {
                $('#Localização').hide();
                $('#Ano').show();
                $('#Semestre').show();
                e.stopPropagation();
            }
        }
    

    Issue found -> e.preventDefault() you have to call after function call(MostraDropDownList).

    and You have to use instead of this use 'myFormType' to pass parameters.

    Thanks.