laravel7 jqAjax下拉框搜尋
阿新 • • 發佈:2021-12-14
html: 設定頁面改變事件
<div id="show">
<div class="page-container" style="width: 300px"> <label >科室下拉搜尋</label> <div> <select name="interest" id="serarch" onchange="serch()" class="input-text" > <option value="老年人套餐">老年人套餐</option> <option value="兒童套餐">兒童套餐</option> </select> </div> </div>
</div>
引入jq,觸發Ajax
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script type="text/javascript"> function serch() { // 獲取下拉框的分類值 var value=$('#serarch').val(); //傳送值 $.ajax({ "url":"/package/serarch", "data":{value}, 'dataType':'json', "type":"POST", success:function (res) { console.log(res)if (res.code==200){ var data = res.data; var str="<table>"; str += "<th>ID</th>"; str += "<th>套餐名稱</th>"; str += "<th>套餐型別</th>"; str += "<th>適用院區</th>"; str += "<th>套餐價格</th>"; str += "<th>套餐專案</th>"; str += "<th>操作</th>"; for (var i=0;i<data.length;i++){ str +="<tr><td>"+data[i].id+"</td><td>"+data[i].Package_name+"</td><td>"+data[i].type+"</td><td>"+data[i].yuan+"</td><td>"+data[i].price+"</td><td>"+data[i].price+"</td><td>"+data[i].project+"</td></tr>" } str+="</table>"; $("#show").html(str); $("tr:odd").css('background-color',''); $("tr:even").css('background-color',''); $("tr:eq(0)").css('background-color',''); $("table").css('border','1px solid red'); } } }) } </script>
設定路由
//下拉搜尋 Route::post('package/serarch','PackageController@serarch');
控制器:
public function serarch(Request $request){ // 接受值 $data=$request->post('value'); // 進行查詢 $res=Package::where('type',"=","$data")->get(); // 返回結果 return ['code'=>200,'data'=>$res,'msg'=>'success']; }
效果圖