简体   繁体   中英

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>

I have fixed some of the issues in your code. Now it will not give you errors and you can modify your html as you want. Also show and hide will work.

There is no need to pass extra parameter with the function call every time. The better way is to call e.stopPropagation(); after function call.

<html>
<head></head>
<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>

            </p>
        </div>
    </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $('#Localização').hide();
        $('#Ano').hide();
        $('#Semestre').hide();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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