php 使用按钮 onclick 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22950781/
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
redirect using button onclick
提问by Chris
I am using the Laravel framework and the blade templating engine.
我正在使用 Laravel 框架和刀片模板引擎。
What I would like to do, is have 2 buttons in my view, when clicked will redirect you to another view. The code I tried was:
我想做的是在我的视图中有 2 个按钮,单击时会将您重定向到另一个视图。我试过的代码是:
<button type="button" onclick="{{ Redirect::to('users.index') }}">Button</button>
回答by The Alpha
You may try this (assumed this code in in a blade template):
你可以试试这个(假设这个代码在刀片模板中):
<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
However, {{ url('users/index') }}
will print the url
so, it'll become something like this in the browser:
但是,{{ url('users/index') }}
将打印url
so,它会在浏览器中变成这样:
<button type="button" onclick="window.location='http://domain.com/users/index'">Button</button>
It means that, you have a route declared something like this:
这意味着,您有一条路线声明如下:
Route::get('users/index', array('uses' => 'UserController@index', 'as' => 'users.index'));
In this case, may also use:
在这种情况下,也可以使用:
<button type="button" onclick="window.location='{{ route("users.index") }}'">Button</button>
Out put will be same.
输出将是相同的。
回答by msurguy
Yeah, you would need to basically use the URL helper to generate the URL (same as just putting something like http://yoursite.com/usersinstead of the PHP helper):
是的,您基本上需要使用 URL helper 来生成 URL(与放置类似http://yoursite.com/users 之类的内容而不是 PHP helper 相同):
<button type="button" onclick="window.location='{{ URL::route('users.index'); }}'">Button</button>
Although I don't see why you can't just use an "a href" tag instead of the button, like this:
虽然我不明白为什么你不能只使用“a href”标签而不是按钮,如下所示:
<a href="{{ URL::route('users.index'); }}">My button</a>