Javascript重定向并将参数传递给重定向的页面 [英] Javascript redirect and Pass Argument to redirected Page

查看:123
本文介绍了Javascript重定向并将参数传递给重定向的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个按钮的主页面。每个按钮应该重定向到同一页面,但是,根据您按下的按钮,您重定向到的页面应该做不同的事情。如何将用户重定向到新页面,并根据您点击哪个按钮传递参数?

浏览器事件处理对于像JQuery这样的工具包简单得多:

HTML:

 < button data-param =foo> Foo< / button> 
< button data-param =bar> Bar< / button>

JQuery代码:

  $('button')。click(function(){
window.location = window.location.href +'?param ='+ $(this).data('param') ;
});

演示: http://jsfiddle.net/6DK5J/


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?

解决方案

Cross-browser event handling is much simpler with a toolkit like JQuery:

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');
});

Demo: http://jsfiddle.net/6DK5J/

这篇关于Javascript重定向并将参数传递给重定向的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆