How to take the value of several select in the same change

Asked

Viewed 158 times

0

Basically, I have a button that adds two fields when clicked, an input number and a select:

<div class="geraInput">
    <div id="form-conjunto">
        <select class="input select mb-1" name="produto[]">
            <optgroup label="Selecione o produto">
               <?php
                    foreach ($dados as $produtos):
               ?>
                <option value="<?php echo $produtos['id']?>"><?php echo $produtos['nome_produto']?></option>
                <?php endforeach; ?>
            </optgroup>
        </select>
        <input type="number" name="qtd[]" min="0" class="input number" placeholder="Quantidade" required><br/><br/>
    </div>
    </div>
$(document).ready(function () {
    //Ao clicar no botão, adiciona uma nova div de inputs
    $('.addInput').click(function () {
        //Clona a div e salva na variável clone
        var clone = $('.form-conjunto').clone();
        //Gera a nova div
        $('.geraInput').append(clone);
    });
});

I have another script that dynamically takes the value of select so that the input number has a max according to the product selects, I do this via ajax but, jQuery is only taking the value of the first select and replicating in all inputs.

 $('.select').change(function () {
        var max = $('.select').val();
        $.ajax({
            type: 'GET',
            url: 'AjaxReq/baixa.php',
            data: {id: max},
            success:function (html) {
                $('select').parent().find('.number').attr('max', html);
            }
        });
    });

1 answer

0

Since you were using the class selector to pick the max value, it always takes the first one from the screen. Swap to take the value of the select being changed, using the $(this):

$('.select').change(function () {
    var max = $(this).val();
    ...
});
  • It worked, I changed the 2 selectors for this. Thanks

Browser other questions tagged

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