admin管理员组文章数量:1336644
My route:
Route::get('deleterequest/{request_id}', 'RequestController@getDeleteRequest')->name('getDeleteRequest');
In view, i want to click a tag to call this route View:
<a onclick=" confirmDelete({{ $request->task_id }})" href="#" ><span class="fa fa-trash-o"></span></a>
<script type="text/javascript">
function confirmDelete(id){
document.location.href="{!! route('getDeleteRequest', $id); !!}";
}
when i run the code, it show error message:
Undefined variable: id
My route:
Route::get('deleterequest/{request_id}', 'RequestController@getDeleteRequest')->name('getDeleteRequest');
In view, i want to click a tag to call this route View:
<a onclick=" confirmDelete({{ $request->task_id }})" href="#" ><span class="fa fa-trash-o"></span></a>
<script type="text/javascript">
function confirmDelete(id){
document.location.href="{!! route('getDeleteRequest', $id); !!}";
}
when i run the code, it show error message:
Share Improve this question asked Mar 28, 2018 at 2:49 Trọng KoyTrọng Koy 211 gold badge1 silver badge2 bronze badgesUndefined variable: id
2 Answers
Reset to default 3I just you need to parse it and replace..
function confirmDelete(id){
let url = "{{ route('getDeleteRequest', ':id') }}";
url = url.replace(':id', id);
document.location.href=url;
}
or just
<a onclick="confirmDelete({{route('getDeleteRequest', $request->task_id) }})" href="#" ><span class="fa fa-trash-o"></span></a>
function confirmDelete(url){
document.location.href=url;
}
goto your_project_folder\config and create file constants.php if it's not exist and then define site_path constant inside it
define('SITE_PATH', 'http://www.yoursite/');
- and then use like this
window.location = SITE_PATH+'deleterequest/'+id;
本文标签: how to call laravel route from javascript functionStack Overflow
版权声明:本文标题:how to call laravel route from javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408994a2469404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论