Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <form action='/my_path/' method='GET'>
  2.   <div class='aclass'>
  3.     <input type='text' size='50' placeholder='Type something' />
  4.     <button type='submit'>Click me</button>
  5.   </div>
  6. </form>
  7.          
  8. <script>
  9.   function success(data, textStatus, jqXHR)
  10.   {
  11.     alert('DEBUG: success');
  12.   }
  13.  
  14.   function error(jqXHR, textStatus, errorThrown)
  15.   {
  16.     alert('DEBUG: error');
  17.   }
  18.  
  19.   jQuery.fn.ajaxify = function(selector, success, error)
  20.   {
  21.     if (arguments.length === 2)
  22.     {
  23.       error = success;
  24.       success = selector;
  25.       selector = undefined;
  26.     }
  27.  
  28.     return this.on('submit', function(e)
  29.     {
  30.       // Copy the options from the '<form />' control.
  31.       jQuery.ajax({
  32.         url: this.target,
  33.         method: this.method || 'GET',
  34.         data: '{SomeJSON: ' + (typeof selector === 'undefined' ? $(this).serialize() : $(this).find(selector).serialize()) + '}'
  35.       }).done(jQuery.proxy(this, success)).fail(jQuery.proxy(this, error));
  36.  
  37.       e.preventDefault();
  38.     });
  39.   };
  40.  
  41.   jQuery(document).ready(function()
  42.   {
  43.     jQuery('.button').click(function()
  44.     {
  45.       jQuery.fn.ajaxify('input', success, error);
  46.     });
  47.   });        
  48. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement