简体   繁体   中英

jquery: anchor class click fired multiple times

I am new to jquery. I wish to clone a div, with new id, on anchor click. But when I click on the anchor element, the div is cloned multiple times.

Code:

$(document).on("click", "a.cls-copy", function(event){
               event.stopPropagation();
               div_id = $(this).closest('div').attr('id');
               var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
               console.log("newdiv : "+$(newdiv).attr('id'));
               if($(newdiv).find(".questiondata").length != 0)
               {
               $(newdiv).find(".questiondata").val("");
               }

               e_id = "edit"+countPage+"-"+questionCount;
               var temp_id= $(newdiv).attr('id');

               d_id = "del"+countPage+"-"+questionCount;
               c_id = "copy"+countPage+"-"+questionCount;
               questionCount++;

               showSuccessToast("Your question is copied");
               $("#"+temp_id).find(".cls-edit").attr('id',e_id);
               $("#"+temp_id).find(".cls-delete").attr('id',d_id);
               $("#"+temp_id).find(".cls-copy").attr('id',c_id);

               });

html

html+='<li><a data-role="button" class="km-widget km-button cls-delete" type="button" id="'+del_id+'" ><span class="km-text">delete</span></a>&nbsp;&nbsp;<a data-role="button" class="km-widget km-button cls-copy" type="button" id="'+copy_id+'"><span class="km-text">copy</span></a>&nbsp;&nbsp;<a data-role="button" class="km-widget km-button cls-edit" type="button" id="'+edit_id+'"><span class="km-text">edit</span></a></li>';

Where am I getting wrong? How do I solve this?

Found the solution:

 $(document).off("click","a.cls-copy").on("click", "a.cls-copy", function(event){
               event.stopPropagation();
               div_id = $(this).closest('div').attr('id');
               var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
               console.log("newdiv : "+$(newdiv).attr('id'));
               if($(newdiv).find(".questiondata").length != 0)
               {
               $(newdiv).find(".questiondata").val("");
               }

               e_id = "edit"+countPage+"-"+questionCount;
               var temp_id= $(newdiv).attr('id');

               d_id = "del"+countPage+"-"+questionCount;
               c_id = "copy"+countPage+"-"+questionCount;
               questionCount++;

               showSuccessToast("Your question is copied");
               $("#"+temp_id).find(".cls-edit").attr('id',e_id);
               $("#"+temp_id).find(".cls-delete").attr('id',d_id);
               $("#"+temp_id).find(".cls-copy").attr('id',c_id);

               });

With this code,

1) .on - you can add elements to your DOM and still handle click event.

2) .off - remove event handler

Refer http://www.gajotres.net/prevent-jquery-multiple-event-triggering/

   $(document).ready(function(){

        $("a.cls-copy").unbind('click').bind("click", function(event){
           alert("test");
           event.stopPropagation();
           div_id = $(this).closest('div').attr('id');
           var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
           console.log("newdiv : "+$(newdiv).attr('id'));
           if($(newdiv).find(".questiondata").length != 0)
           {
           $(newdiv).find(".questiondata").val("");
           }

           e_id = "edit"+countPage+"-"+questionCount;
           var temp_id= $(newdiv).attr('id');

           d_id = "del"+countPage+"-"+questionCount;
           c_id = "copy"+countPage+"-"+questionCount;
           questionCount++;

           showSuccessToast("Your question is copied");
           $("#"+temp_id).find(".cls-edit").attr('id',e_id);
           $("#"+temp_id).find(".cls-delete").attr('id',d_id);
           $("#"+temp_id).find(".cls-copy").attr('id',c_id);

           });
     });
  Replace your code with above code and check how much time test alerts ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM