【问题标题】:Laravel button does not link to routeLaravel 按钮未链接到路由
【发布时间】:2016-10-30 03:38:43
【问题描述】:
@extends('layout')

@section('content')
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1>{{ $id->title }}</h1>

            <ul class="list-group">
                @foreach($id->notes as $note)
                    <li class="list-group-item">
                        {{ $note->body }}
                        <span class="pull-right">
                                <button href="/notes/{{ $note->id }}/edit" type="button" class="label label-default pull-xs-right">Edit</button>
                        </span>
                    </li>
                @endforeach
            </ul>

            <hr>
            <h3>Add a New Note</h3>

            <div class="form-group">
                <form method="post" action="/cards/{{ $id->id }}/notes">
                    <div class="form-group">
                        <textarea name="body" class="form-control"></textarea>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Add Note</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@stop

foreach 循环中的按钮具有正确的链接 /note/id/edit。我可以将其输入 chrome 并转到编辑页面。但是,按钮并没有指向那里。我只看到点击动画。为什么会这样?

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    Button 元素没有href 属性,因此您可以将其包装在这样的链接标签中。这是最简单的方法

    <a href="{{"/notes/". $note->id. "/edit"}}" >
    <button type="button" class="label label-default pull-xs-right">Edit</button
    ></a>
    

    或者您可以将其包装在表单元素中,并将 action 属性设置为您想要的链接,如下所示。

    <Form method="get" action="{{"/notes/". $note->id. "/edit"}}">
    <button type="submit" class="label label-default pull-xs-right">Edit</button>
    </Form>
    

    第三种方法是使用 javascript 和 onclick 监听器。

    【讨论】:

    • 我使用了第二个建议,但必须将按钮类型更改为“提交”。谢谢!
    【解决方案2】:

    使用&lt;a&gt;&lt;/a&gt;标签,而不是button,并设置type="button"

     <a href="{{ route('change.anything') }}" type="button"> Check Any </a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2021-05-01
      • 2020-02-16
      • 2022-01-17
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      相关资源
      最近更新 更多