javascript - Ajax executing multiple times -


i have written ajax post request seems executing multiple times. first time click button execute once, second time click button run through code twice, 3 times execute 3 times , on. not sure causing problem, here ajax post request. if other information needed happily provide.

$(document).ready(function () { $('#posteditdatasource').click(function (event) {     //serialise , assign json data hidden field     $('#dsdeleteddp').val(json.stringify(deleted));     $('#dsediteddp').val(json.stringify(editdparr));      //get form     var form = $('#__dsajaxantiforgeryform');      var url = 'settings/editdatasource';      $('#__dsajaxantiforgeryform').on('submit', function () {         $.ajax({             url: url,             data: form.serialize(),             type: 'post',             success: function (result) {                  reloadposteditaction($('#dsid').val());                  if (deleteddatapoints != null) {                     deletefromtable(deleted);                 }                  //clear values hidden inputs                 $('input:hidden').each(function () {                     if ($(this).attr('name') != '__requestverificationtoken' && $(this).attr('id') != 'dsid') {                         $(this).val('');                     }                 });                  $('#dsediteddp').val('');                  showdatasourcepostalert('#successpost', 3000);             },             error: function (jqxhr, textstatus, errorthrown) {                 //alert(jqxhr + ', ' + textstatus + ', ' + errorthrown);                 showdatasourcepostalert('#successpost', 3000);             }         })         return false;     }) }); }) 

the problem experience expected code.

every time event happens: $('#posteditdatasource').click(function (event) {

you add new event here: $('#__dsajaxantiforgeryform').on('submit', function () {

so, events compound. every time click #posteditdatasource element, assign submit event handler #__dsajaxantiforgeryform

in other words, first time click, have 1 submit event handler. second time click, have 2 submit event handlers. third time click, have 3 submit event handlers, , on...

you can fix removing submit event handler first (by using .off(), $('#__dsajaxantiforgeryform').off().on('submit', function () {


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -