【问题标题】:Categories and subcategories with AJAX at Laravel project not working?Laravel 项目中使用 AJAX 的类别和子类别不起作用?
【发布时间】:2020-02-02 17:45:34
【问题描述】:

我无法在更改类别选择表单时选择子类别

$.ajax({
           type:"GET",
           url:"{{url('get-categorie-list')}}?cat_id="+cat_id,
           dataType:"json",

【问题讨论】:

  • 你有比模糊描述和 4 行代码更多的信息吗(甚至不完整,因为我们看不到你的 ajax 定义的结尾)?
  • 我是 stackoverfolw 的新手,我不知道如何显示我所有的代码
  • 好的,如果你直接使用编辑器中的代码并将其粘贴到左对齐,然后选择它并按 ctrl+k。这会将您的选择格式化为code

标签: ajax laravel laravel-5


【解决方案1】:

文件 wib.php

Route::get('get-categorie-list/{cat_id}','AnnonceController@getSubcatList');

html

<div class="form-group">
    <label for="cat" class="col-md-3 control-label">Catégorie</label>
    <div class="col-sm-6">
      <select name='categorie_id' class="form-control" id='categorie' placeholder = 'Sélectionner votre categorie'>
        <option>Sélectionner votre categorie</option>
       @foreach($categories as $categorie)
       <option value="{{ $categorie->id }}"> {{ $categorie->name }}</option>
       @endforeach
       </select>

    </div>

  </div> <!-- form-group // -->
  <div class="form-group">
    <label for="cat" class="col-md-3 control-label">Sous Catégorie</label>
    <div class="col-sm-6">
      <select name='subcategorie_id' class="form-control" id="subcategorie">


       </select>

    </div>

  </div> 
  <div class="form-group">

我的控制器:

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\State;
use App\Categorie;
use App\SubCategorie;
use App\Http\Requests;

use DB;
class AnnonceController extends Controller
{

public function getSubcatList(Request $request)
    {


            $subcategories =SubCategorie::where('categorie_id','=',$request->cat_id)->get();
        return response()->json($subcategories);

    }

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 2017-09-05
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多