2014-01-22 58 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