Take Select Option and multi-line Checkbox values

Asked

Viewed 144 times

0

I need to make a list of employees who are linked to cost centers and I need to tell you if this employee is leadership within this cost center.

I tried to mount a select and an added checkbox by clicking the +button, but I can’t get the corresponding values with jquery.

Here is the html

<div class="form-group">
            <label for="intIdCentroCusto" class="col-lg-2 control-label"></label>
            <div class="col-lg-8">
                <table class="col-lg-12 col-md-12">

                    <tr ><td class="bd_titulo" width="10">Centro de Custo</td><td class="bd_titulo">Liderança</td></tr>
                    <tr class="linhas"  style="width: 800px">
                        <td  style="width: 2000px">
                            <select data-minlength-error='Selecione pelo menos um'
                                                data-placeholder='Digite ou selecione' data-minlength='1'
                                                multiple='multiple' name='arrCentroCusto[]'
                                                id='arrCentroCusto'
                                                class="js-basic-multiple form-control" required>
<option value="1">Centro custo 1</option>
<option value="2">Centro custo 2</option>
<option value="3">Centro custo 3</option>
</select>
                        </td>
                        <td style="width: 300px">
                            <div class="checkbox">
                                <label>
                                    <input name='boolLideranca[]' value='1' <?php echo $this->modelo->boolLideranca==1 ? 'checked' : ''; ?> type="checkbox" class="boolLideranca"> Sim
                                </label>
                            </div>
                        </td>
                        <td><a href="#" class="removerCampo" title="Remover linha"><img src="imagens/details_close.png" border="0" /></a></td>
                    </tr>

                    <tr><td colspan="4">
                            <a href="#" class="adicionarCampo" title="Adicionar item"><img src="imagens/details_open.png" border="0" /></a>
                        </td>
                </table>
        </div>

and here I am trying to get the variables with jquery

var lideranca = [] ; // inicia o array;
var arrCentroCusto = []; // inicia o array

b = 0;
i = 0; // ÍNDICE INICIAL DO ARRAY
$('#arrCentroCusto option:selected').each(function() {
    arrCentroCusto[i] = $(this).val(); // PEGANDO OS IDS DAS OPÇÕES SELECIONADAS
    i++; // INCREMENTANDO O ÍNDICE
});

$(".boolLideranca:checked").each(function() {
    lideranca[b] = $(this).val(); // PEGANDO OS IDS DAS OPÇÕES SELECIONADAS
    b++;
});

I need to take this information to save on the table the cost center and if it is leadership. inserir a descrição da imagem aqui

  • where is the code that adds the elements by clicking on the "+"? Your selector to find the selected items is starting with id ("#arrCentroCusto"), as you can only have one element with this ID, as you will check the others select? wouldn’t it be better to use a class for this? Why ID will only read the first

  • There are ecrita errors in your Html, select has not been closed.

  • This is not the original code, the original is closed and within a php, I just put an example there.

  • I put the . arrCentroCusto and still only search 1 value

  • Why I need to tell leadership by cost center and not in general. @Diogo

1 answer

0


I solved the problem, but I had to change the checkbox to a select to work.

I’ll leave the answer here for anyone who wants to use it in the future.

<div class="form-group">
        <label for="intIdCentroCusto" class="col-lg-2 control-label"></label>
        <div class="col-lg-8">
            <table class="col-lg-12 col-md-12">

                <tr ><td class="bd_titulo" width="10">Centro de Custo</td><td class="bd_titulo">Liderança</td></tr>
                <tr class="linhas"  style="width: 800px">
                    <td  style="width: 2000px">
                        <select data-minlength-error='Selecione pelo menos um'
                                            data-placeholder='Digite ou selecione' data-minlength='1'
                                            multiple='multiple' name='arrCentroCusto[]'
                                            id='arrCentroCusto'
                                            class="js-basic-multiple form-control" required>
                                            <option value="1">Centro custo 1</option>
                                            <option value="2">Centro custo 2</option>
                                            <option value="3">Centro custo 3</option>
                        </select>
                    </td>
                    <td style="width: 300px">
                        <select name='boolLideranca[]'
                                        class='form-control boolLideranca' required>
                                    <option value="0">Não</option>
                                    <option value="1">Sim</option>
                    </td>
                    <td><a href="#" class="removerCampo" title="Remover linha"><img src="imagens/details_close.png" border="0" /></a></td>
                </tr>

                <tr><td colspan="4">
                        <a href="#" class="adicionarCampo" title="Adicionar item"><img src="imagens/details_open.png" border="0" /></a>
                    </td>
            </table>
    </div>

    <script>
    var lideranca = [] ; // inicia o array;
        var arrCentroCusto = []; // inicia o array

        //var contador = ($("tr.linhas").length);

        var i = 0;
        $('.arrCentroCusto').each(function() {
            arrCentroCusto[i] = $(this).val(); // PEGANDO OS IDS DAS OPÇÕES SELECIONADAS
            i++; // INCREMENTANDO O ÍNDICE
        });

        var b = 0;
        $('.boolLideranca').each(function() {
            lideranca[b] = $(this).val(); // PEGANDO OS IDS DAS OPÇÕES SELECIONADAS
            b++;
        });
    </script>

Browser other questions tagged

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