Created
February 5, 2013 11:16
-
-
Save yeikos/4713805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
// Enlazamos todos los botones | |
$('body').on('click.fixEOTarget', 'input[type=submit], button', function() { | |
var $self = $(this), | |
$form = $self.closest('form'), | |
remove = function() { | |
// Eliminamos el elemento .fixEOTarget | |
$('.fixEOTarget', $form).remove(); | |
}, name; | |
// Si el elemento pertenece a un formulario | |
if ($form) { | |
// De primeras nos deshacemos del elemento .fixEOTarget | |
remove(); | |
$('.fixEOTarget', $form).remove(); | |
// Si el elemento sobre el que se pulsó tiene nombre | |
if ((name = $self.attr('name')) && name.length) | |
// Añadimos al formulario el elemento .fixEOTarget con dicho nombre y valor | |
$('<input type="hidden" class="fixEOTarget" />').attr({ | |
name: name, value: $self.val() | |
}).appendTo($form); | |
// Damos un margen de 200 ms y eliminamos el elemento .fixEOTarget | |
setTimeout(remove, 200); | |
/* | |
El setTimeout se debe a que no tenemos el control cuando se envía el formulario desde otros eventos, | |
como un submit desde un input[type=text], en ese caso, .fixEOTarget no debe estar definido, ya que | |
no se pulsó sobre ningún botón. Es por ello que damos un margen pequeño para que el evento submit capte | |
el valor de .fixEOTarget y lo eliminamos a continuación, para evitar el problema descrito. | |
*/ | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment