oprogfrogo
oprogfrogo

Reputation: 2074

jQuery - Adding hidden input to form on submit

I'm looking for a way to insert a hidden input on submitting for 'Yes' in my jquery code below.

How do I insert this:

<input type="hidden" name="token" value="1">

From this:

$(function() {
    $( "#dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "Yes": function() {
                $( '#form' ).submit();
            },
            "No": function() {
                $( '#form' ).submit();
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
});

Upvotes: 5

Views: 27090

Answers (3)

Peter Kellner
Peter Kellner

Reputation: 15508

I'm new to JQuery but thought others might appreciate this for adding multiple values.

var myFormData = [
    {
        name: 'x_login',
        value: 'xxxxxxxxxx'
    }, {
        name: 'x_amount',
        value: donationAmount
    }
];

$.each(myFormData, function (index, myData) {
    debugger;
    $('#registerformid').append('<input type="hidden" name=' + myData.name + ' value=' + myData.value + '>');
});

$('#registerformid').submit();

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this

$(function() {
    $( "#dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "Yes": function() {
                $('#form').append('<input type="hidden" name="token" value="1" />').submit();
            },
            "No": function() {
                $('#form').submit();
            },
            Cancel: function() {
                $(this).dialog( "close" );
            }
        }
    });
});

Upvotes: 14

Johan
Johan

Reputation: 3212

Easy answer, first adding the input and then calling submit (I'm guessing a bit of context btw).

$('#form').append('<input type="hidden" name="token" value="1">').submit();

Upvotes: 7

Related Questions