jquery - Multiple Ajax request by 1 click -


when have:

$('#blah').on('click', function(){     $.ajax({         //blah     }) }); 

i works fine, above code make ajax request once, when have:

$('body').on('click', '#blah', function(){     $.ajax({         //blah     }) }); 

this 1 sends ajax request many times, sometime twice , more. , have use second code since #blah object added page ajax request, first code doesn't work.

what problem? have used return false; @ end of code, not working... .

i appreciate kind of help.


more info:

let's have container, , there buttons inside container, container has loaded earlier ajax request, like:

$('a[rel="show_product_detail"]').on('click', function(){         var product_id = $(this).attr('id');         $('.companycontent').html('');         $('.companycontent').addclass('members_content_loading');         $('.companycontent').load('modules/companies/companies_tabs/product_details.php?product_id='+ product_id', function() {             $('.companycontent').removeclass('members_content_loading');         });         return false;        }); 

now in container have buttons cause new content container, buttons have use second code since buttons loaded via ajax. clicking on buttons inside container, ajax requests send many times(depends on times container has reloaded).

after " success" fired in first request, add dynamic content page, once done ,after code bind click listener

$.ajax({ //...  success:function (data){ $(data).appendto(".companycontent"); //inserts #blah $("#blah").bind("click",function(){ //ajax code  })  }); 

or can build new object , make instances when need

function ajax(){ this.url=""; this.control=""; this.run = run;` }  function run(){  $.ajax({  url:this.url,  success:function (data){ $(data).appendto(".companycontent"); //inserts #blah $("#blah").bind("click",function(){ // }); } }); 

and new instance , init object. object nice if modify better

var test= new ajax(); test.url="myajax.com/ajax"; test.control=get_news(); // example click event test.run(); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -