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();

});

尝试首先将表单附加到body元素:

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();