Variable JS inside html tag inside Laravel Blade

Asked

Viewed 172 times

-1

I need to make the value collected in the option, complete the action of my form. It would be something similar to this idea, but is not returning the desired.

<form class="form-horizontal" id="pform" action="/registrar/{valor}" method="post" enctype="multipart/form-data" name="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Registros</h5>
                    </div>
                    <div class="modal-body">

                        <input type="hidden" id="id" class="form-control">

                        <div class="form-group">
                            <label for="user_id" class="control-label">Tipo de registro</label>
                            <div class="input-group">
                                <select class="form-control" id="tipo_id" onchange="option(this.value)">
                                    <option value="1">Entrada</option>
                                    <option value="2">Saida</option>
                                    <option value="3">Retirada</option>
                                    <option value="4">Despesas Fixas Mensais</option>
                                    <option value="5">Despesas Fixas Anuais</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="nomeEntrada" class="control-label">Descrição</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="nomeEntrada" placeholder="Nome da entrada">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="valorEntrada" class="control-label">Valor</label>
                            <div class="input-group">
                                <input type="number" class="form-control" id="valorEntrada" placeholder="Valor da entrada">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="user_id" class="control-label">Usuário</label>
                            <div class="input-group">
                                <select class="form-control" id="user_id" >
                                    <option value="{{ Auth::user()->id }}">{{__(Auth::user()->name)}}</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Salvar</button>
                        <button type="cancel" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                    </div>
                </form> </p>
            </div>
        </div>
    </div>

    <script>
        function option(valor){

            console.log(valor);
        }
    </script>

1 answer

0

One solution I found is to define the action value according to which the select is changed, thus:

function option(valor) {
    $('#pform').attr('action', '/registrar/' + valor);
}

Also, you should change the main action to:

action="/registrar/1"

Since 1 is the default value of your select.

Browser other questions tagged

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