2013-02-12 106 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