javascript HTML表单提交后如何返回上一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32343044/
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
How to get back to previous page after HTML form submission
提问by VaTo
I have this HTML code which contains two HTML forms and two buttons: One for submit and one for cancel (basically get back to the previous page). What I would like to do is after I click the SUBMIT button and submit my forms do the same as the cancel button (get back to the previous page). The problem is that I can't specify what page I want it to go back since this page will be used in many pages so it will be dynamic.
我有这个 HTML 代码,它包含两个 HTML 表单和两个按钮:一个用于提交,一个用于取消(基本上返回上一页)。我想要做的是在我点击 SUBMIT 按钮并提交我的表单后做与取消按钮相同的操作(返回上一页)。问题是我无法指定我希望它返回哪个页面,因为此页面将在许多页面中使用,因此它将是动态的。
<div data-role="page" id="customer" data="Page" data-expired="true" data-theme="c">
<div data-role="content" data="customers" data-key="id" data-params="item:itemid">
<div align="left">
<h3 style="margin:0px">Customer Profile</h3>
</div>
<br/>
<div class="popup-hrule">
<div data-role="collapsible" data-collapsed="false" data-collapsed-icon="false" data-theme="a">
<h3>Customer</h3>
<form id="submit-edit-customer" method="post" data-formaction="process">
<label for="customer_name">Customer</label>
<input type="text" name="customer_name" id="customer_name" data-item=":customer_name">
</div>
<div class="ui-block-b">
<label for="name">Primary Contact</label>
<input type="text" name="name" id="name" value="no valid name"
data-item=":name">
</div>
</form>
</div>
<div data-role="collapsible" data-collapsed-icon="false" data-theme="a">
<h3>Billing</h3>
<form id="submit-edit-customer" method="post" data-formaction="process">
<label for="customer_name">Customer</label>
<input type="text" name="customer_name" id="customer_name" data-item=":customer_name">
</div>
<div class="ui-block-b">
<label for="name">Primary Contact</label>
<input type="text" name="name" id="name"
data-item=":name">
</div>
</form>
</div>
</div>
<a id="createeditcancel" data-role="button" data-inline="true" href="#" data-rel="back">Cancel</a>
<a id="submit-edit-customer-btn" data-role="button" data-inline="true" data-theme="a">Save</a>
</div>
</div>
My question is, is there any easy way to do this without writing a lot of javascript code in the onclick event?
我的问题是,有没有什么简单的方法可以做到这一点,而无需在 onclick 事件中编写大量 javascript 代码?
Thanks.
谢谢。
回答by Vexxoz
Very easy all you have to do is change the href to the url of previous page:
非常简单,您只需将 href 更改为上一页的 url:
<a id="createeditcancel" data-role="button" data-inline="true" href="YOUR_URL_HERE" data-rel="back">Cancel</a>
Edit: if your submit is using php to save the info just add
编辑:如果您提交的是使用 php 来保存信息,只需添加
header("Location: last_page.html");
Since its dynamic you can just get the back URL from the same place as the rest of your info and do:
由于它是动态的,您可以从与其他信息相同的位置获取返回 URL,然后执行以下操作:
header("Location:" + URL);
回答by VaTo
I didn't know I could do this but I tried it and it worked!
I added: data-rel="back"
to the submit button.
我不知道我能做到这一点,但我试过了,它奏效了!我添加了: data-rel="back"
到提交按钮。
Like this:
像这样:
<a id="submit-edit-customer-btn" data-role="button" data-inline="true" data-rel="back" data-theme="a">Save</a>
回答by Aleksandar Milisavljevic
For redirect to other page in JavaScript you can use
要重定向到 JavaScript 中的其他页面,您可以使用
window.location = "http://www.yoururl.com";
add to button onclick="location.href = 'www.yoursite.com';"
添加到按钮 onclick="location.href = 'www.yoursite.com';"
where u handle your form?php file?
你在哪里处理你的表格?php文件?
other option in php
php 中的其他选项
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
or in form
或形式
<form action="demo_form.asp" onsubmit="myFunction()">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
location.href = 'www.yoursite.com';
}
</script>
回答by Aleksandar Milisavljevic
With document.referrer
:
与document.referrer
:
var cancel = function() {
window.location.href = document.referrer;
};
<input type="button" value="Cancel" onclick="cancel()" />
With history
:
与history
:
var cancel1 = function() {
history.back();
};
var cancel2 = function() {
history.go(-1);
};
<input type="button" value="Cancel" onclick="cancel1()" />
<input type="button" value="Cancel" onclick="cancel2()" />
回答by Leo Javier
on the action page or your controller try adding this:
在操作页面或您的控制器上尝试添加以下内容:
$(document).ready(function(){
document.location = 'url' // the path to your homepage
});