Check checkbox with returns in DIV

1

In this Form:

<input type="checkbox" value="1" data-id=1" name="status_entrega" id="status_entrega">
<div id="retorno_1" style="display:none; float:left">Atualizado</div>

<input type="checkbox" value="1" data-id=2" name="status_entrega" id="status_entrega">
<div id="retorno_2" style="display:none; float:left">Atualizado</div>

I have the following jQuery:

$('#status_entrega').click(function(){
    var id_pedido = $("#status_entrega").attr("data-id");
    $("#retorno_"+id_pedido).toggle(this.checked);
});

However, the first item I can mark as checked, the second and the others, I can not even mark ... Does not return the update message. Can anyone help?

Update:

According to Sergio's help, I modified it, but I need you to uncheck the update request as well. Here is the updated code:

$('[name="status_entrega"]').click(function() {
    var id_pedido = $(this).attr("data-id");
    $("#retorno_" + id_pedido).toggle(this.checked);

    $.ajax({
        type: 'post',
        url: '../ajax/getTransacaoOK',
        success: function (response) {

            $("#retorno_" + id_pedido).html("Atualizando...");
            setTimeout(function(){

                $("#retorno_" + id_pedido).html("Atualizado!");

                setTimeout(function(){
                    $("#retorno_" + id_pedido).html("");
                },1000);

            },2000);    
        },
    });         
});
    
asked by anonymous 18.12.2016 / 13:25

1 answer

1

You have duplicate IDs ... they have to be unique .

You can use name="status_entrega" as a selector and do so:

$('[name="status_entrega"]').click(function() {
    var id_pedido = $(this).attr("data-id");
    $("#retorno_" + id_pedido).toggle(this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><section><inputtype="checkbox" value="1" data-id="1" name="status_entrega" id="status_entrega">
    <div id="retorno_1" style="display:none; float:left">Atualizado</div>
</section>
<section>
    <input type="checkbox" value="1" data-id="2" name="status_entrega" id="status_entrega">
    <div id="retorno_2" style="display:none; float:left">Atualizado</div>
</section>
    
18.12.2016 / 13:57