javascript - jquerymobile. double execute function -
i have such code:
function getdata(){ $.ajax({ url: 'http://pechati.ru/extdata/baseparts/filialswsunsec.php', contenttype: "application/x-www-form-urlencoded;charset=utf8", datatype: 'json', success: function(jsondata){ //alert('load performed.'); console.debug('data received.'); //jsondata = win2unicode(jsondata); console.debug(jsondata); $.each(jsondata,function (filial,datafilial) { // //console.debug(filial); var option = '<option value="'+filial+'" data-phone="'+ datafilial.phone+ '" data-email="' + datafilial.email + '"> ' + filial + '</option>'; //filial active var activefilial = 0; if (parseint(datafilial.mainlist)>0){ activefilial++; } if (parseint(datafilial.contacts)>0){ activefilial++; } if (parseint(datafilial.uslugi)>0){ activefilial++; } if (activefilial>0){ $('#filial-name').append(option); } }); }, error: function(error){ console.debug('we have problem: '); } }); } ... $(document).bind('pageinit', function() { $('a.get-order').on('click', function () { getdata(); }); }
getdata executing twice after click on a.get-order it. why happening it? element placed in :
<div id="one"> ... </div> <div id="two"> ... <a class="get-order">...</a> </div>
the first page loaded "#one". called page "#two". after click on a.get-order have double execution getdata(). if calling page "#three" page "#two" , execute function there executed 3 times. why happening , how fix it?
your code:
$(document).bind('pageinit', function() { $('a.get-order').on('click', function () { getdata(); }); }
runs every time page initialized. click handler added once when #one initialized , again when #two initialized. change to
$(document).on("pageinit","#two", function(){ $('a.get-order').on('click', function () { getdata(); }); });
this way click handler added once when page #two initialized.
Comments
Post a Comment