jQuery动态创建和提交表单

时间:2013-02-12 16:01:51

标签: jquery

我正在尝试通过代码创建并提交表单。下面有什么问题?

$('#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();

});

1 个答案:

答案 0 :(得分:29)

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

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