【发布时间】:2018-04-15 08:52:48
【问题描述】:
我想用AJAX 更改div 的内容(我不想在单击链接时重新加载所有页面),我找到了一些文档,但我只看到“静态”解决方案(它们是“硬编码”的,即“如果你点击这个,就会带来这个”,但我不想在我的项目底部使用 3000 行开关盒)。
有人可以向我展示一个“动态”解决方案,我只需将控制器、操作和参数提供给点击按钮,jquery 路由器无需修改即可进行路由?
我的示例代码:
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
@include('includes.head')
</head>
<body>
<div id="header">
<nav id="navbar" class="navbar navbar-default">
<ul class="nav nav-tabs navbar-right">
<li>
<a action="FirstExampleController@firstExamle" params="[a => 24, b => 52]">
<button type="button" class="btn btn-link">First Example</button>
</a>
</li>
<li>
<a action="SecondExampleController@secondExamle" params="[id => 1, newValue => 42]">
<button type="button" class="btn btn-link">Second Example</button>
</a>
</li>
</nav>
</div
<div id="app">
<!-- This will be changed by the router -->
</div>
<footer class="container navbar">
@include('includes.footer')
</footer>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
控制器动作
class FirstExampleController extends Controller{
public function firstExample(Request $request){
$a = $request -> a;
$b = $request -> b;
$c = $a + $b;
return $c;
}
}
class SecondExampleController extends Controller{
public function secondExample(Request $request){
$id = $request -> id;
$newValue = $request -> newValue;
//database operation where the id's object's new value will be $newValue
return $this->showItems;
}
}
【问题讨论】:
标签: php ajax laravel laravel-5.5