Función Jquery ANTES del envío del formulario


Estoy intentando activar una función usando Jquery cuando se hace clic en el botón enviar formulario, pero la función necesita activarse ANTES de que el formulario sea enviado.

Estoy tratando de copiar algunos atributos de etiquetas div en campos de texto ocultos al enviarlos, y luego enviar el formulario.

He logrado que esto funcione usando la función mouseover (cuando el botón submit se pasa el cursor sobre), pero esto no funcionará en dispositivos móviles usando touch.

$("#create-card-process.design #submit").on("mouseover", function () {
    var textStyleCSS = $("#cover-text").attr('style');
    var textbackgroundCSS = $("#cover-text-wrapper").attr('style');
    $("#cover_text_css").val(textStyleCSS);
    $("#cover_text_background_css").val(textbackgroundCSS);
});

He jugado con el .enviar función, pero los valores no se guardan dentro de los campos, ya que la función se activa cuando se envía el formulario y no antes.

Muchas gracias

Author: starsplusplus, 2014-02-21

5 answers

Puede utilizar el onsubmit función.

Si devuelve false, el formulario no se presentó. Lea sobre ello aquí.

$('#myform').submit(function() {
  // your code here
});
 78
Author: kartikluke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-21 15:46:58
$('#myform').submit(function() {
  // your code here
})

Lo anterior es NO trabajando en Firefox. El formulario simplemente se enviará sin ejecutar su código primero. También se mencionan cuestiones similares en otras partes... como esta pregunta. El trabajo alrededor será

$('#myform').submit(function(event) {

 event.preventDefault(); //this will prevent the default submit

  // your code here

 $(this).unbind('submit').submit(); // continue the submit unbind preventDefault
})
 34
Author: Waqas Bukhary,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:30

Puede usar algún div o span en lugar de botón y luego hacer clic en llamar a alguna función que envía formulario al final.

<form id="my_form">
   <span onclick="submit()">submit</span>
</form>

<script>
   function submit()
   {   
       //do something
       $("#my_form").submit();
   }
</script>
 3
Author: Skriptotajs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-21 15:51:39

Aghhh... me faltaba algún código cuando lo intenté por primera vez .enviar función.....

Esto funciona:

$('#create-card-process.design').submit(function() {
var textStyleCSS = $("#cover-text").attr('style');
var textbackgroundCSS = $("#cover-text-wrapper").attr('style');
$("#cover_text_css").val(textStyleCSS);
$("#cover_text_background_css").val(textbackgroundCSS);
});

Gracias por todos los comentarios.

 2
Author: Matt Price,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-21 15:51:36

Solo porque cometí este error cada vez que usé la función submit.

Este es el código completo que necesita:

Agregue el id " yourid" a la etiqueta del formulario HTML.

<form id="yourid" action='XXX' name='form' method='POST' accept-charset='UTF-8' enctype='multipart/form-data'>

El código de jQuery:

$('#yourid').submit(function() {
  // do something
});
 0
Author: meck373,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-26 14:17:00