How to clear an event

Asked

Viewed 38 times

1

I am creating a comment system with reply only that at the time of reply goes twice to the bank when I send a comment, I made the system of response in slide where you open one and close the other, only when you open the next one to comment and close the previous one is still loaded and counts as if it were two answers someone knows how to solve ?

code

    function addEventosRespostas() {
        $(document).on("click", ".resposta", function(){
            var id = $(this).children('input').val();

            $('.coment').find('.resp').fadeOut("slow");
            $(this).parents('.coment').find('.resp').fadeIn("slow");

            $(document).on("submit","form", function() {

                var texto = $(this).children('textarea').val();
                var coment_id = $(this).find('#id_coment').val();
                var id_user_coment = $(this).find('#id_user_coment').val();

                    $.post('swith/viwer.php',{
                        acao: 'resposta', texto: texto, coment_id: coment_id, id_user_coment: id_user_coment},
                            function(){
                                $('.dialog').fadeIn();
                                $('form textarea').val("");
                                $('form input[type = submit]').val("responder");
                                $('.closemodal').click(function(){
                                        $('.dialog').fadeOut();
                                });
                            });
            return false;
            });

        });
    }

// Exibir mais comentários



    $('#load').on("click",function(){

        loadmo = $(this);
        var video = $('#link').val();
        var numcom = $('.comentar .com').length;

        $.post('swith/viwer.php',{
            acao:'comentarios', video: video, numcom: numcom},function(contcom){

            if(contcom => 1){
                $.post('swith/viwer.php',{
                    acao:'morecoment', video: video, numcom: numcom},function(morecom){

                    $('.comentar .com:last').after(morecom);
                    $('.comentar .com').fadeIn("slow");
                    addEventosRespostas();





                }); 

            }

            if(contcom > 1){

            }else{
               loadmo.fadeOut("fast"); 
            }
        });
    });

// Responder Comentário        



        addEventosRespostas();

Php code

<div class='comentar  fl-left'>
                        <div class='coment com' >

                                <?php 

                    if($foto == "" || $foto == "uploads/"):
                        echo  "<a href='user?id={$id}'><img class='fl-left' src='".REQUIRE_PATH."/css/boot/icons/thumb.png'></a>";
                    else:
                        echo  "<a href='user?id={$id}'><img class='fl-left' src='{$imx['foto']}'></a>";
                    endif;
                    ?>
                   <p><small class='fontze1'>Comentado por:</small> <?=$nome?> <small>Em:  </small> <?=$date?> <small>As: </small> <?=$hora?></p>

                   <div class="comentando">
                       <p><?=nl2br($comentario)?></p></div>
                       <div class='fl-right resposta'>Responder<input type='hidden' value='<?=$big[id] ?>' name='idcoment'>
                       </div>
                       <div class=' resp' style='display:none'>
                           <form method='post' id="comentarios" class="formulario" >
                               <input type="hidden" value="<?= $big['id'] ?>" id="id_coment" >
                               <input type="hidden" value="<?= $_SESSION['mail'] ?>" id="id_user_coment" >
                               <textarea id="texto"></textarea>  

                               <input class='btn btn-green' type='submit' value='responder' name='responder'>
                           </form>
                       </div>
                   </div>
  • You can put the HTML you have so we can respond better?

1 answer

0

This is happening because you add the event:

 $(document).on("submit","form",function());

twice, so if a form der Ubmit it will fall in both understand? I made an example where you will clearly see this happening.

Example

A solution would only be to add the code one time :

 $(document).on("submit","form",function());

So you’ll get all the "form" submits. I hope I have helped, in case you have any doubts or need me to explain you better can comment below.

  • so in case I don’t have two events

  • You call the function addEventsRespos() how many times ? have to post more code ?

  • I just posted now for better exemplification

Browser other questions tagged

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