2013-02-12 104 views
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(); 

}); 
+1

你可能想在jQuery的。员额http://api.jquery.com/jQuery.post/看看吧。这可能更接近你正在寻找的东西。 – 2013-02-12 16:05:11

+0

“下面有什么问题?”我不知道,你有什么错误?什么不是你认为应该发生的事情? – j08691 2013-02-12 16:06:57

回答

29

尝试的形式附加到body元素第一:

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

啊,先得到appendTo身体。这工作!提交后是否值得从体内移除? – Mark 2013-02-12 16:10:51

+2

@Mark否,因为页面将被卸载,然后表单被提交。 – Corneliu 2013-02-12 16:12:44