Position all selects at index 0 with Jquery

Asked

Viewed 102 times

1

Good morning!

I have a question, and I was wondering if anyone could help me. I have two selects on my page. I have a button to register a new record and another button to edit each record. When I click on the edit, it opens a modal that contains the information from that record. If then I click on the "New Registration" button, it continues to leave the selects with the information from the latest record. Thanks for your help!

Below, the modal code.

<div class="modal fade" id="modal-edit-veiculo" tabindex="-1" role="dialog" aria-labelledby="modal-edit-veiculo-label" aria-hidden="true">
    <div class="modal-dialog" style="width:80% !important;">

        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Fechar</span>
                </button>
                <h4 class="modal-title" id="modal-edit-veiculo-label">Novo Veículo</h4>
            </div><!-- modal header -->

            <div class="modal-body">

                <form class="form-horizontal">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label" for="unidade-veiculo">Unidade</label>
                            <div class="col-sm-8">
                                <select class="form-control modal-detalhe-veiculo-unidade-veiculo" name="unidade-veiculo">
                                    <option>Selecione uma Unidade</option>
                                    <option value="001S000000nlKu4IAE">Teste 2</option>
                                    <option value="001S000000nmTBdIAM">Inpulso 04</option>
                                    <option value="001S000000nm2EiIAI">Teste Inpulso 8</option>
                                    <option value="001S000000nm2deIAA">Teste Inpulso 10</option>
                                    <option value="001S000000nm2fGIAQ">Teste Inpulso 14</option>
                                    <option value="001S000000kHf7lIAC">RMR COMERCIO E TRANSPORTE DE GAS LTDA.</option>
                                    <option value="001S000000nJfoyIAC">Santos</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label" for="placa">Placa</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control modal-detalhe-veiculo-placa-veiculo" name="placa" placeholder="Placa"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="col-sm-4 control-label" for="tipo-veiculo">Tipo de Veículo</label>
                            <div class="col-sm-8">
                                <select class="form-control modal-detalhe-veiculo-tipo-veiculo" name="tipo-veiculo">
                                    <option>Selecione um Tipo de veículo</option>
                                    <option value="a1vS0000000CLYkIAO">Caminhonete</option>
                                    <option value="a1vS0000000CLYpIAO">Triciclo</option>
                                    <option value="a1vS0000000CLYfIAO">Pick-Up</option>
                                    <option value="a1vS0000000CLYuIAO">Caminhão</option>
                                    <option value="a1vS0000000CLYzIAO">Moto</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label" for="descricao-veiculo">Descrição</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control  modal-detalhe-veiculo-descricao-veiculo" name="descricao-veiculo" placeholder="Descrição"/>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="clearfix"></div>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal" id="btn-salvar-veiculo">Gravar</button>
            </div>

        </div>
    </div>
</div>

Below, the button that opens the modal:

<div class="col-xs-12" style="margin-top: 10px;">
        <div class="form-group" style="border: 1px">
            <div id="" class="col-xs-12 " align="right">
                <button id="btn-novo-veiculo" class="btn btn-primary">Novo Cadastro</button>
            </div>
        </div> 
    </div>

the function to clear the fields:

function clearModal(){
            j$('#modal-edit-veiculo .modal-body input').val('');
        }

The inputs are being cleaned, now I need to position the selects index 0 when you click the button.

  • Friend explain your doubt better, if possible post a code from an example of what you are trying to do

2 answers

0


In his job clearModal(), add the following line:

j$('#modal-edit-veiculo .modal-body select').prop('selectedIndex', 0);

Thus remaining:

function clearModal() {
    j$('#modal-edit-veiculo .modal-body input').val('');
    j$('#modal-edit-veiculo .modal-body select').prop('selectedIndex', 0);
}

Fiddle. Source.

  • Thank you very much, that’s exactly what I needed!

0

One time you got those input and select in a <form> the best thing would be to form.reset();...

document.querySelector('form').reset();

jsFiddle: http://jsfiddle.net/xm37qbfh/

Browser other questions tagged

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