当我从所有帖子的下拉列表中选择帖子时,我试图能够重定向到该帖子所在的页面。
目前,我已经设法用所有帖子标题填充列表,并且我已经以 a 的形式向列表中的每个元素添加了一个链接,但是当我单击其中一个元素时,什么也没有发生。
这是我用来填充列表的代码:
<div class="col-md-10">
<select class="form-control" id="post" name="post" required focus>
@foreach($post as $posts)
@if($posts->user_id == Auth::user()->id)
<option value="/posts/{{$posts->id}}" selected>
<a href="/posts/{{$posts->id}}">
{{$posts->title}}
</a>
</option>
@endif
@endforeach
</select>
</div>
我需要重定向的页面采用以下格式:
/posts/id
其中 id 是帖子 id
我该如何进行这项工作?
你必须这样使用
<a class="dropdown-item" href="{{url('')}}/posts/{{$posts->id}}">{{$posts->title}}</a>
我通过将选项值设置为我想要重定向的链接/路由并放置 onchange 事件来解决它。
onchange="window.location.href=this.value;"
<div class="col-md-10">
<select class="form-control" id="post" name="post" required focus onchange="window.location.href=this.value;">
@foreach($post as $posts)
<option value="{{route('your-route', $posts->id)}}">{{$posts->title}}</option>
@endforeach
</select>
</div>