[英]Submit form with three actions
I have a three-button form. 我有一个三按钮形式。 First button "only Save document", second button "only generate PDF" and third button "Save document and Generate PDF". 第一个按钮“仅保存文档”,第二个按钮“仅生成PDF”,第三个按钮“保存文档并生成PDF”。
only Save button - just submitting form - works fine 仅保存按钮-仅提交表单-可以正常工作
<form method="post">
<button type="submit" name="save">
<input>....
</form>
only Generate PDF button - change form action and target with simple jquery and send it to PDF generator - also works fine 仅生成PDF按钮-使用简单的jquery更改表单操作和目标,并将其发送到PDF生成器-也可以正常工作
<form method="post">
<button type="submit" name="generate" id="generate">
<input>....
</form>
<script>
$('#generate').click(function(){
$('form').get(0).setAttribute('action', './library/pdf/generator.php');
$('form').get(0).setAttribute('target', 'blank');
});
</script>
Save document and Generate PDF button - here i need merge both in one on one click... submit + save data and open PHP generator in new tab(window) .. both with same POST data ... Any ideas? 保存文档并生成PDF按钮-在这里我需要一键合并两者...提交+保存数据并在新选项卡(窗口)中打开PHP生成器..两者都具有相同的POST数据...有什么想法吗? I'm stuck here 我被困在这里
EDIT: Tried this callback without luck.. it open only pdf generator (im begginer in jquery, so maybe its nonsence) 编辑:试过了没有运气的回调。.它仅打开pdf生成器(在jQuery中是乞gg,所以可能是不存在的)
$(function() {
$('#saveandgenerate').click(function(){
$('form').get(0).setAttribute('action', './library/pdf/generator.php');
$('form').get(0).setAttribute('target', 'blank');
$('form').get(0).submit(function(){
$('form').get(0).setAttribute('action', '');
$('form').get(0).setAttribute('target', '');
$('form').get(0).submit();
});
});
});
I suggest using a simple ajax post than javascript to submit your form. 我建议使用比javascript简单的ajax发布来提交表单。
<form method="post" id="myForm">
<button type="button" id="generate" onclick="save_data();">
Generate PDF
</button>
<input>....
</form>
<script>
function save_data(){
$.ajax({
url: 'first/post/location/url',
type: 'post',
data: $("#myForm").serialize(),
success: function (response) {
setTimeout(function (){
document.getElementById("myForm").submit();
}, 1000);
}
});
}
</script>
Hope this helps. 希望这可以帮助。
Working solution: 工作解决方案:
$(function() {
$('#save_and_generate').click(function()
{
$('form').get(0).setAttribute('action', './library/pdf/generator.php');
$('form').get(0).setAttribute('target', 'blank');
$('form').get(0).submit();
setTimeout(function() {
$('form').get(0).setAttribute('action', '');
$('form').get(0).setAttribute('target', '');
$("button#save").trigger('click');
},100);
});
});
Submit with "Save and generate PDF" > change form atributes and open corectly new window with PDF > After 100 timeout change form atributes to default > trigger "only Save" button 使用“保存并生成PDF”提交>更改表单属性,并使用PDF打开一个新窗口> 100次超时后,更改表单归功于默认设置>触发“仅保存”按钮
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.