javascript Javascript重定向并将参数传递给重定向的页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7774731/
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
Javascript redirect and Pass Argument to redirected Page
提问by Saher Ahwal
I have a main page that contains two buttons. Each of the buttons should redirect to the same page, however, the page that you are redirected to should do different things depending on the button you pressed. How can I redirect the user to that new page and pass in an argument depending on which button you have clicked?
我有一个包含两个按钮的主页。每个按钮都应该重定向到同一页面,但是,您被重定向到的页面应该根据您按下的按钮执行不同的操作。如何将用户重定向到该新页面并根据您单击的按钮传入参数?
回答by AlienWebguy
Cross-browser event handling is much simpler with a toolkit like JQuery:
使用像 JQuery 这样的工具包,跨浏览器事件处理要简单得多:
HTML:
HTML:
<button data-param="foo">Foo</button>
<button data-param="bar">Bar</button>
JQuery code:
查询代码:
$('button').click(function(){
window.location = window.location.href + '?param=' + $(this).data('param');
});
回答by Clive
It depends what you mean by 'pass in an argument' but if you mean by URL query string then you can simply set up a form with an action to the page in question with a GET
method:
这取决于您所说的“传入参数”是什么意思,但如果您的意思是 URL 查询字符串,那么您可以简单地使用一种GET
方法设置一个表单,其中包含对相关页面的操作:
<form action="page.html" method="GET">
<input type="submit" name="submit1" value="Submit1" />
<input type="submit" name="submit2" value="Submit2" />
</form>
The redirected URL will be page.html?submit1=Submit1
if the first button is clicked and page.html?submit2=Submit2
if the second button is clicked.
page.html?submit1=Submit1
如果单击第一个按钮和单击page.html?submit2=Submit2
第二个按钮,则重定向的 URL 将是。