jQuery-añadir parámetros adicionales al enviar (NO ajax)


Usando 'submit' de jQuery - ¿hay alguna manera de pasar parámetros adicionales a un formulario? NO estoy buscando hacer esto con Ajax - esto es normal, actualización típica de envío de formulario.

$('#submit').click(function () {
    $('#event').submit(function () {
        data: { 
        form['attendees'] = $('#attendance').sortable('toArray').toString();
    });
});
Author: GSerg, 2010-03-27

6 answers

Este lo hizo por mí:

var input = $("<input>")
               .attr("type", "hidden")
               .attr("name", "mydata").val("bla");
$('#form1').append(input);

Se basa en la respuesta del Daff, pero se agregó el atributo NAME para que se muestre en la colección de formularios y se cambió el VALOR a VAL También comprobó el ID del FORMULARIO (form1 en mi caso)

Usó Firefox firebug para verificar si el elemento se insertó.

Los elementos ocultos se vuelven a publicar en la colección de formularios, solo se descartan los campos de solo lectura.

Michel

 321
Author: Michel,
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
2018-09-10 06:56:11

En su caso, debería bastar con agregar otro campo oculto a su formulario dinámicamente.

var input = $("<input>").attr("type", "hidden").val("Bla");
$('#form').append($(input));
 25
Author: Daff,
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
2013-03-27 01:17:34

Incluso puedes usar este. funcionó bien para mí

$("#registerform").attr("action", "register.php?btnsubmit=Save") 
$('#registerform').submit();

Esto enviará el valor btnsubmit =Save as GET al registro.formulario php.

 14
Author: Parag,
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
2012-09-11 07:11:58

Puede escribir una función jQuery que le permita agregar campos ocultos a un formulario:

// This must be applied to a form (or an object inside a form).
jQuery.fn.addHidden = function (name, value) {
    return this.each(function () {
        var input = $("<input>").attr("type", "hidden").attr("name", name).val(value);
        $(this).append($(input));
    });
};

Y luego agregue el campo oculto antes de enviar:

var frm = $("#form").addHidden('SaveAndReturn', 'Save and Return')
                    .submit();
 10
Author: Jonathan,
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
2013-03-26 11:24:14

No es necesario enlazar el evento submit con el clic del botón submit solo enlazar el evento submit y capturará el evento submit sin importar cómo se desencadene.

Piensa que lo que quieres es enviar el clasificable como lo harías a través de ajax. Intenta hacer algo como esto:

var form = $('#event').submit(function () {
    $.each($('#attendance').sortable('toArray'),function(i, value){
        $("<input>").attr({
            'type':'hidden',
            'name':'attendace['+i+']'
        }).val(value).appendTo(form);
    });
});
 10
Author: PetersenDidIt,
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-08 16:10:06

Respuesta similar, pero solo quería que estuviera disponible para una prueba fácil/rápida.

var input = $("<input>")
               .attr("name", "mydata").val("go Rafa!");

$('#easy_test').append(input);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>



<form id="easy_test">
  
</form>
 2
Author: Cacho Santa,
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
2015-09-15 00:46:57