Hide or show div when editing a form

0

I would like you to help me with this question and how to solve it. I have 2 div, the second div is hidden depending on what I select in the first div, until then everything is fine, because I can hide or show the div.

<div class="row">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <label for=""> Tiene Seguro</label>
                                    <select name="seguro" id="seguro">
                                        <option value="1" @if($user->seguro==1) selected @endif>SI</option>
                                        <option value="2" @if($user->seguro==2) selected @endif>NO</option>
                                    </select>
                                 </div>
                            </div>
                        </div>

<div class="row" id="tipoSeguro" style="display:none">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <div class="form-check">
                                        <label class="form-check-label"><input  class="form-check-input" name="tipo" type="radio" value="1" @if($user->tipo_seguro==1) checked @endif>Médico - Dental</label>
                                    </div>
                                    <div class="form-check">
                                        <label class="form-check-label"><input class="form-check-input" name="tipo" type="radio" value="2" @if($user->tipo_seguro==2) checked @endif>Otros</label>
                                    </div>
                                </div>
                            </div>
                        </div>

<script>
    $(document).ready(function (){
        $('#seguro').append(function (){

            if($(this).val()==1){
                $('#tipoSeguro').show();
            }else if($(this).val()==2){
                $('#tipoSeguro').hide();
            }else if($(this).val()==""){
                $('#tipoSeguro').hide();
            }

        });
     });
</script>

My problem comes when I want to edit the form and the div that must be shown is shown or hidden, but if I change the option of the secure selection by YES, the hidden div should be displayed, before I had it with a click (function () ), but I did not do anything if the secure select was YES, it was not shown, I changed it to the append, but now it does not change if you selected Yes or NO.

Help, thanks

    
asked by Juanjo Medina 25.06.2018 в 16:11
source

2 answers

0

What I would do is change the way you hide the div, and when you ask to show or hide use the fadeIn (show) and fadeOut (hide)

<div class="row">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label for=""> Tiene Seguro</label>
                                <select name="seguro" id="seguro">
                                    <option value="1" @if($user->seguro==1) selected @endif>SI</option>
                                    <option value="2" @if($user->seguro==2) selected @endif>NO</option>
                                </select>
                             </div>
                        </div>
                    </div>

<div class="row" id="tipoSeguro" @php echo ($user->seguro==2)?'style="display:none"':''; @endphp>
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="form-check">
                                    <label class="form-check-label"><input  class="form-check-input" name="tipo" type="radio" value="1" @if($user->tipo_seguro==1) checked @endif>Médico - Dental</label>
                                </div>
                                <div class="form-check">
                                    <label class="form-check-label"><input class="form-check-input" name="tipo" type="radio" value="2" @if($user->tipo_seguro==2) checked @endif>Otros</label>
                                </div>
                            </div>
                        </div>
                    </div>


<script>
$(document).ready(function (){
    $('#seguro').change(function (){

        if($(this).val()==1){
            $('#tipoSeguro').fadeIn();
        }else if($(this).val()==2){
            $('#tipoSeguro').fadeOut();
        }else if($(this).val()==""){
            $('#tipoSeguro').fadeOut();
        }

    });
 });

I hope your answer will help

    
answered by 25.06.2018 / 19:41
source
0

Try using "change" instead of "append" . Since the "change" method detects the change in the value of the item you select, it can interact with any item that contains a value and this value can be altered at any time. Then your code would be left like this:

<div class="row" id="tipoSeguro">
        <div class="col-sm-12">
            <div class="form-group">
                <div class="form-check">
                    <label class="form-check-label"><input  class="form-check-input" name="tipo" type="radio" value="1" @if($user->tipo_seguro==1) checked @endif>Médico - Dental</label>
                </div>
                <div class="form-check">
                    <label class="form-check-label"><input class="form-check-input" name="tipo" type="radio" value="2" @if($user->tipo_seguro==2) checked @endif>Otros</label>
                </div>
            </div>
        </div>
    </div>

 <select name="seguro" id="seguro">
    <option selected>---</option>
    <option value="1">SI</option>
    <option value="2">NO</option>
 </select>


<script>
    $(document).ready(function (){
        $('#tipoSeguro').hide(); //Ocultas el div

        $('#seguro').on("change", function (){

          var otr= event.target.value; //otr tomara el value que tenga cada etiqueta <option>

            if(otr==1){
                $('#tipoSeguro').show();
            }else if(otr==2){
                $('#tipoSeguro').hide();
            }
        });
     });
</script>

The problem that you were having and that I had not realized was that you had a style="display: none" on the div with the id="safeType", therefore no matter what you did in the script was always going to have a hidden display. I hope now if it serves you, let me know anything;)

    
answered by 25.06.2018 в 16:32