Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery create and submit form dynamically

Tags:

jquery

I'm trying to create and submit a form through code. What's wrong with the below?

$('#btnPrintInvoicePerFile').on('click', function (event) {
    event.preventDefault(); //stop link default

    var componentInvoiceId = EberlsComponentInvoiceForm.componentInvoice.Id;
    console.log(componentInvoiceId);

    $('<form>', {
        "id": "getInvoiceImage",
        "html": '<input type="text" id="componentInvoicId" name="componentInvoicId" value="' + componentInvoiceId + '" />',
        "action": window.siteRoot + 'ComponentInvoice/GetInvoiceImage/'
    }).submit();

});
like image 228
Mark Avatar asked Feb 12 '13 16:02

Mark


1 Answers

Try to append the form to the body element first:

$('<form>', {
    "id": "getInvoiceImage",
    "html": '<input type="text" id="componentInvoicId" name="componentInvoicId" value="' + componentInvoiceId + '" />',
    "action": window.siteRoot + 'ComponentInvoice/GetInvoiceImage/'
}).appendTo(document.body).submit();
like image 182
Corneliu Avatar answered Oct 13 '22 05:10

Corneliu