Create a dynamic value in checkbox using ajax

1

I have a ajax code link and I need the values that appear to be dynamic, eg:

In the link that I put has 3 inputs and in js I created it for only 1, if I put it to all it would send normally

    checkbox:$($("#checkbox input")[0]).prop("checked")?$($("#checkbox input")[0]).val():'',

I just need these values to be dynamic, that is, I do not need to put 1 by 1. I would only put 1. that it would assign the value of each input automatically without having to re-tune that code.

Is it possible and how can I resolve this?

I'm waiting for you

    
asked by anonymous 19.06.2015 / 23:39

1 answer

1

For your HTML it is an idea that all inputs are descendants of this #checkbox div.

So you can use .find() to find the inputs and then using .filter() and .map() create an array with the values of those that are checked. Then you can send this as JSON in AJAX. I also used the .get() of jQuery to give me a native array and work with only native JavaScript from there.

Something like this:

$('#checkbox').on('click', function () {
    var checkboxes = $(this).find('input[name="checkbox[]"]').get().filter(function (input) {
        return input.checked;
    }).map(function (input) {
        return input.value;
    });
    $.ajax({
        type: "POST",
        url: "teste4.php",

        data: {
            checkbox: JSON.stringify(checkboxes),
            profissional: $("#profissional").val()
        },
    
19.06.2015 / 23:50