Event on (change) on radio button

1

In the project I have a file that asks for freight rates and at the same time returns two radio buttons with the freight values. The HTML return is as follows:

<p>
    <b>Selecione: </b><br>
    <input type="radio" name="tipocep" value="1;40.50" class="seltipocep">
    PAC <b>R$ 40,50</b> prazo de 28 dia(s)<br> <br>
    <input type="radio" name="tipocep" value="2;129.40" class="seltipocep">
    SEDEX <b>R$129,40</b> prazo de 4 dia(s)
    <input type="hidden" name="addfrete" value="1"></p>

This HTML content is returned through the following AJAX request:

$("form#consultafrete").submit(function(event) {
    /* Act on the event */
    event.preventDefault();

    <?php if($pagina == 'carrinho'){ ?>
    var tipoCEP = 100;
    <?php }else{ ?>
    var tipoCEP = '';
    <?php } ?>

    var totalItens = <?php echo $_SESSION['carrinho']['total_itens'] ?>;
    var $legendacep = $("#legendacep");
    var meucepCalc = $("#seucep").val();

    if(meucepCalc != ''){

        $legendacep.html("Carregando...");

        $.post(
            '<?=$siteUrl?>/calcula-frete.php',
            { meucep: meucepCalc, tipo_saida: tipoCEP, qtd: totalItens },
            function(data) {
                var resultado = '<p>'+data+'</p>';                      
                $legendacep.html(resultado);                        
            });
        }
    });

What I intend to do is that when the return has radio buttons, when I select one of them, I can do some action, already tried in various ways but it does not work. The fact that it does not work is because the content is dynamic? One of the ways I tried:

$("input[name=tipocep]").change(function() {                
                alert("sDSDS");
            });
    
asked by anonymous 02.01.2015 / 21:35

1 answer

2

Yes, the problem is delegation because this content was added after JS was read.

You can delegate the event to a relative, for example: #legendacep .

$("#legendacep").on('change', "input[name=tipocep]", function() {                
    alert("sDSDS");
});

Example: link

    
02.01.2015 / 21:52