Get value of Select Option and Checkbox of several lines

0

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

I tried to mount a select and an added checkbox by clicking the + button, but I can not 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 get this information to save the cost center table and if it's lead.

    
asked by anonymous 09.11.2018 / 18:49

1 answer

0

I solved the problem, but I had to change the checkbox for 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>
    
12.11.2018 / 14:54