2014-01-22 57 views
0

我是jquery的新手,對於簡單的問題感到抱歉。我想提出其中有兩個提交按鈕形式...jquery表單在新窗口中提交

<input id="submitBtn" name="action" value="view" type="submit" class="ym-button ym-primary" value="<spring:message code="button.view"/>" title="<spring:message code="button.view" />" />         
<input id="saveBtn" name="action" value="save" type="submit" class="ym-button ym-warning" value="<spring:message code="button.save"/>" title="<spring:message code="button.save" />" /> 

正如你可以看到我有一個名爲submitBtn和saveBtn按鈕。 submitBtn返回html,而saveBtn返回一個pdf。

當submitBtn命中時,我希望表單能夠正常提交併在當前窗口中有響應加載。但是,當saveBtn命中時,我希望當前窗口保持不變,並將pdf加載到新的彈出窗口中。

到目前爲止,我已經試過......

$("#submitBtn").click(function(){ 
    if ($("form[name='shortsAndOversDailyForm']").valid()) { 
     return true; 
    } 
    return false; 
}); 

$("#saveBtn").click(function(){ 
    if ($("form[name='shortsAndOversDailyForm']").valid()) { 
     // specify a unique target name 
     var target = 'windowFormTarget'; 
     // open a new window and name it 
     window.open('', target, 'width=1400,height=900'); 
     // set the target of the form to be 
     // the window name 
     this.setAttribute('target', target); 
     // allow the form to be submitted normally 
     return true; 
    } 
    return false; 
}); 

這樣做的問題是,當我打的保存按鈕,它會打開一個新的窗口,沒有內容和PDF在窗口裝在窗體已提交。

有人可以幫助解決這個問題,所以我可以把pdf加載到彈出窗口中嗎?

感謝

+0

你是如何genereting的PDF? – Yani

+0

是的..請指出或解釋您使用什麼或如何爲我們生成PDF以幫助您。 – Marc

回答

1

this是按鈕,按鈕沒有目標,形式一樣。

this.setAttribute('target', target); 

所以你可能要this.form

this.form.setAttribute('target', target); 

或使用jQuery

$(this).closest("form").attr('target', target); 

$("#saveBtn").click(function(){ 
    var form = $("form[name='shortsAndOversDailyForm']"); 
    if (form.valid()) { 
     var target = 'windowFormTarget'; 
     window.open('', target, 'width=1400,height=900'); 
     //this.form.setAttribute('target', target); 
     form.attr('target', target); 
     return true; 
    } 
    return false; 
}); 
+0

不好意思問。但是你可能會更專注於如何實現功能。我試過這個失敗了。謝謝 – Richie

+0

發生了什麼事?你應該用其他兩個中的一個來代替頂線。 – epascarello

+0

謝謝你。我試圖找到最接近的選項,但無法讓它工作。你給出的東西效果很好,但它看起來像是永久性地改變了表格的目標。現在當我點擊我的#submitBtn時,會打開一個新窗口。我試圖通過爲#submitButton提供完全相同的函數來解決這個問題,而不是target = windowFormTarget,我說target ='_self'。但這不起作用。我仍然收到#submitBtn在新窗口中打開。我怎樣才能解決這個問題? – Richie