【问题标题】:redirect using button onclick使用按钮 onclick 重定向
【发布时间】:2014-05-21 23:03:11
【问题描述】:

我正在使用 Laravel 框架和刀片模板引擎。

我想做的是,在我的视图中有 2 个按钮,点击后会将您重定向到另一个视图。我试过的代码是:

 <button type="button" onclick="{{ Redirect::to('users.index') }}">Button</button>

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你可以试试这个(假设这个代码在刀片模板中):

    <button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>
    

    但是,{{ url('users/index') }} 会打印出url,所以它会在浏览器中变成这样:

    <button type="button" onclick="window.location='http://domain.com/users/index'">Button</button>
    

    这意味着,你有一个声明如下的路由:

    Route::get('users/index', array('uses' => 'UserController@index', 'as' => 'users.index'));
    

    在这种情况下,也可以使用:

    <button type="button" onclick="window.location='{{ route("users.index") }}'">Button</button>
    

    输出将是相同的。

    【讨论】:

      【解决方案2】:

      是的,您基本上需要使用 URL 帮助程序来生成 URL(就像放置 http://yoursite.com/users 之类的东西而不是 PHP 帮助程序一样):

      <button type="button" onclick="window.location='{{ URL::route('users.index'); }}'">Button</button>
      

      虽然我不明白为什么你不能只使用“a href”标签而不是按钮,就像这样:

      <a href="{{ URL::route('users.index'); }}">My button</a>
      

      【讨论】:

        【解决方案3】:

        您还可以将 href 标记设置为按钮的样式。例如 &lt;a class="btn btn-primary" href="{{ route('users.create' }}"&gt;Create&lt;/a&gt; 这有助于避免混合 javascript 和刀片指令。```

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-11
          • 2014-10-06
          • 2015-12-02
          • 2018-01-09
          • 2022-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多