Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action='/my_path/' method='GET'>
- <div class='aclass'>
- <input type='text' size='50' placeholder='Type something' />
- <button type='submit'>Click me</button>
- </div>
- </form>
- <script>
- function success(data, textStatus, jqXHR)
- {
- alert('DEBUG: success');
- }
- function error(jqXHR, textStatus, errorThrown)
- {
- alert('DEBUG: error');
- }
- jQuery.fn.ajaxify = function(selector, success, error)
- {
- if (arguments.length === 2)
- {
- error = success;
- success = selector;
- selector = undefined;
- }
- return this.on('submit', function(e)
- {
- // Copy the options from the '<form />' control.
- jQuery.ajax({
- url: this.target,
- method: this.method || 'GET',
- data: '{SomeJSON: ' + (typeof selector === 'undefined' ? $(this).serialize() : $(this).find(selector).serialize()) + '}'
- }).done(jQuery.proxy(this, success)).fail(jQuery.proxy(this, error));
- e.preventDefault();
- });
- };
- jQuery(document).ready(function()
- {
- jQuery('.button').click(function()
- {
- jQuery.fn.ajaxify('input', success, error);
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement