jQuery动态创建和提交表单

Mar*_*ark 8 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();

});
Run Code Online (Sandbox Code Playgroud)

Cor*_*liu 29

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

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

  • @Mark否,因为页面将卸载然后提交表单. (2认同)