Javascript 将表单提交到另一个页面(与 ACTION 中使用的页面不同)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8731653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:14:20  来源:igfitidea点击:

Submit form to another page (which is different from the page used in ACTION)

javascriptjqueryhtml

提问by Vpp Man

I have a form like this:

我有一个这样的表格:

index.php

索引.php

<form method="post" action="send.php">
  <textarea name="msg" id="msg"></textarea>
  <input type="submit" value="Send" />
</form>

So, if I enter something in textarea and clicked on "Send", it is submitted to "send.php" page. But I want to include another button for previewing it. That is, when this button is clicked, the above form is submitted to "preview.php" which will be opened in a new blank window/tab (original page ie. index.php will be there intact). This is to display a preview of the message, that the user is going to send.

所以,如果我在 textarea 中输入一些东西并点击“发送”,它会被提交到“send.php”页面。但我想包含另一个用于预览它的按钮。也就是说,当这个按钮被点击时,上面的表单被提交到“preview.php”,它将在一个新的空白窗口/选项卡中打开(原始页面,即 index.php 将完好无损)。这是为了显示用户将要发送的消息的预览。

I do not know how to do this.

我不知道该怎么做。

回答by Aaron J Spetner

Use Javascript to temporarily change the action and target:

使用 Javascript 临时更改操作和目标:

<form method="post" action="send.php" id="idOfForm">
  <textarea name="msg" id="msg"></textarea>
  <input type="submit" value="Send" />
</form>
<button onclick="doPreview();">Preview</button>
<script type="text/javascript">
    function doPreview()
    {
        form=document.getElementById('idOfForm');
        form.target='_blank';
        form.action='preview.php';
        form.submit();
        form.action='send.php';
        form.target='';
    }
</script>

回答by Noumenon

There is now an attribute for the submit input that handles this:

现在有一个处理这个提交输入的属性:

<input type="submit" formaction=”differentThanNormalAction.php”>

回答by Diodeus - James MacFarlane

Give your form an ID (form1). The action of the current form can be controlled like this:

为您的表单提供一个 ID (form1)。当前表单的动作可以这样控制:

function setPreview() {
    $('#form1').attr('target','_blank')
    $('#form1').attr('action','http://yourpreviewurl.php')
    $('#form1').submit()
}

function setSubmit() {
    $('#form1').attr('target','')
    $('#form1').attr('action','http://yourposturl.php')
    $('#form1').submit()
}

Have two buttons, both type="button", one to call setPreview and another to call setSubmit

有两个按钮,type="button"一个调用 setPreview 另一个调用 setSubmit

回答by Jakub Arnold

You can use JavaScript to change the action of the form when the button is clicked and then submit it.

您可以使用 JavaScript 更改单击按钮时表单的操作,然后提交它。

Or simply submit the form via AJAX and then redirect after you get a response.

或者简单地通过 AJAX 提交表单,然后在收到响应后重定向。

回答by Bernat

<form onreturn="someJavascriptFunction()" action="" method="">

creating a js function able to open this preview page

创建一个能够打开这个预览页面的js函数