Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Adding hidden input to form on submit

Tags:

jquery

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" );
            }
        }
    });
});
like image 597
oprogfrogo Avatar asked Nov 22 '11 18:11

oprogfrogo


2 Answers

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" );
            }
        }
    });
});
like image 91
ShankarSangoli Avatar answered Oct 09 '22 19:10

ShankarSangoli


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();
like image 25
Johan Avatar answered Oct 09 '22 20:10

Johan