Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function ()  {
  2.     //a global dictionary for xmlhttprequest objects
  3.     ajax_requests = new Array();
  4.    
  5.     //iterate over every div element with classname "submit"
  6.     $('div.submit').each(function() {
  7.         var submit = $(this).children('input[type=submit]').first();
  8.         var id = submit.attr('id');
  9.         var found = id.match(/^submit-([0-9]+)$/);
  10.                  
  11.         if (found != null) {
  12.         var measurement_id = found[1];
  13.  
  14.             var stop_ajax = submit.siblings('h2.stop-ajax').first();
  15.  
  16.             submit.bind('click',function(event) {
  17.                 stop_ajax.css('display','block');
  18.            
  19.                 var url = "<?php echo  $this->Html->url(array('controller' => 'Measurements','action' => 'ajax')); ?> ";
  20.                 url +="/"+measurement_id;
  21.                 url = url.replace(/\s/g,'');
  22.  
  23.                 var ajax = $.ajax({
  24.             data: $(this).closest("form").serialize(),
  25.                     dataType: "html",
  26.                     success: function(data,textStatus) {
  27.                         $('#element'+measurement_id).html(data);
  28.                          stop_ajax.css('display','none');
  29.                    },
  30.                    type: "post",
  31.                     url: url
  32.                 });
  33.                 //add the created xmlhttprequest object into the global ajax_requests dictionary
  34.         ajax_requests[measurement_id] = ajax;
  35.  
  36.                 return false;
  37.          });
  38.  
  39.      stop_ajax.bind('click',function(event) {
  40.          var request = ajax_requests[measurement_id];
  41.          if(!(request === undefined)) {
  42.                  request.abort();
  43.                  delete ajax_requests[measurement_id];
  44.              }
  45.               $(this).css('display','none');
  46.          });
  47.         }
  48.     });
  49. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement