简体   繁体   English

仅在按下特定的提交按钮时才将表单提交到新窗口吗?

[英]Submit form to new window only when specific submit button is pressed?

I have a form, which has a few different submit buttons on it all doing different things with the same post data. 我有一个表单,上面有几个不同的提交按钮,它们都用相同的发布数据来做不同的事情。

Lets say for simplicity sake the form looks like this: 为了简单起见,我们将表单如下所示:

<form method="post">
  <input type="hidden" name="ids" value="1,2,3,4" />
  <input type="submit" id="picking" name="picking" value="Picking" />
  <input type="submit" id="shipping" name="shipping" value="Shipping" />
  <input type="submit" id="invoice" name="invoice" value="Invoice" />
</form>

At the moment the form submits to itself and I work out server side which submit button is pressed, build a URL from the POST data, then do a PHP redirect to what I need to go. 目前,表单提交给自己,我在服务器端确定按下了哪个提交按钮,从POST数据构建URL,然后将PHP重定向到我需要执行的操作。 This works fine. 这很好。

However, I am looking for the form to post its data to a new window, but only when "invoice" is clicked. 但是,我正在寻找将其数据发布到新窗口的表单,但是仅当单击“发票”时才可以。 This rules out just adding target="_blank" to the form, as the other 2 buttons would submit to new pages as well. 这排除了仅向表单添加target="_blank"可能性,因为其他两个按钮也将提交到新页面。

I also can't split the form into 3 different forms as the data is a lot more complex than the above, and a lot of it is input by the user. 我也不能将表单分为3种不同的表单,因为数据比上面的复杂得多,并且很多数据是由用户输入的。

Is there a way to do this using JavaScript/JQuery? 有没有办法使用JavaScript / JQuery做到这一点? If so, where would I start? 如果是这样,我将从哪里开始?

Thanks 谢谢

could you not add target blank to the form when invoice is clicked?: 单击发票后,是否不能将目标空白添加到表单?:

$("#invoice").click(function(){
  $('#form_id').attr('target', '_blank');
});

or: 要么:

$(document).on("click","#invoice",function(){
      $('#form_id').attr('target', '_blank');
    });

Try adding a click handler to the correct submit button. 尝试将点击处理程序添加到正确的提交按钮。

$('#invoice').on('click', function(){
    //doStuff
});

This will allow you to control the action of #invoice without affecting the others. 这样您就可以控制#invoice的操作,而不会影响其他发票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM