Laravel:Ajax动态依赖下拉列表

时间:2018-12-29 23:14:52

标签: javascript ajax laravel

当我在选择框中选择特定国家/地区时,则必须在状态选择框中填充所选国家/地区的州数据,依此类推。但是选择国家(父选择框)后,省(子)仍然为空。事件(在浏览器控制台中)传递所选值,然后给出错误(请参见结果)。

我用不同的laravel版本尝试了此代码:5.4-5.7。我对AJAX不太满意。

CountryController.php


<?php

namespace App\Http\Controllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;

use App\Provinces;
use App\Regencies;
use App\Districts;
use App\Villages;


class CountryController extends Controller
{
public function provinces(){
  $provinces = Provinces::all();
  return view('country.index', compact('provinces'));
}

public function regencies(){
  $provinces_id = Input::get('province_id');
  $regencies = Regencies::where('province_id', '=', $provinces_id)->get();
  return response()->json($regencies);
}

public function districts(){
  $regencies_id = Input::get('regencies_id');
  $districts = Districts::where('regency_id', '=', $regencies_id)->get();
  return response()->json($districts);
}

public function villages(){
  $districts_id = Input::get('districts_id');
  $villages = Villages::where('district_id', '=', $districts_id)->get();
  return response()->json($villages);
 }
}

型号


地区:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Districts extends Model
{
protected $table = 'districts';
}

省:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Provinces extends Model
{
 protected $table = 'provinces';
}

Regencies:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Regencies extends Model
{
 protected $table = 'regencies';
}

Villages:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Villages extends Model
{
protected $table = 'villages';
}

查看:index.blade.php


      <div class="form-group">
        <label for="">Your Provinces</label>
        <select class="form-control" name="provinces" id="provinces">
          <option value="0" disable="true" selected="true">=== Select Provinces ===</option>
            @foreach ($provinces as $key => $value)
              <option value="{{$value->id}}">{{ $value->name }}</option>
            @endforeach
        </select>
      </div>

      <div class="form-group">
        <label for="">Your Regencies</label>
        <select class="form-control" name="regencies" id="regencies">
          <option value="0" disable="true" selected="true">=== Select Regencies ===</option>
        </select>
      </div>

      <div class="form-group">
        <label for="">Your Districts</label>
        <select class="form-control" name="districts" id="districts">
          <option value="0" disable="true" selected="true">=== Select Districts ===</option>
        </select>
      </div>

      <div class="form-group">
        <label for="">Your Villages</label>
        <select class="form-control" name="villages" id="villages">
          <option value="0" disable="true" selected="true">=== Select Villages ===</option>
        </select>
      </div>

    </form>
  </div>
</div>


<script type="text/javascript">
  $('#provinces').on('change', function(e){
    console.log(e);
    var province_id = e.target.value;
    $.get('/json-regencies?province_id=' + province_id,function(data) {
      console.log(data);
      $('#regencies').empty();
      $('#regencies').append('<option value="0" disable="true" selected="true">=== Select Regencies ===</option>');

      $('#districts').empty();
      $('#districts').append('<option value="0" disable="true" selected="true">=== Select Districts ===</option>');

      $('#villages').empty();
      $('#villages').append('<option value="0" disable="true" selected="true">=== Select Villages ===</option>');

      $.each(data, function(index, regenciesObj){
        $('#regencies').append('<option value="'+ regenciesObj.id +'">'+ regenciesObj.name +'</option>');
      })
    });
  });

  $('#regencies').on('change', function(e){
    console.log(e);
    var regencies_id = e.target.value;
    $.get('/json-districts?regencies_id=' + regencies_id,function(data) {
      console.log(data);
      $('#districts').empty();
      $('#districts').append('<option value="0" disable="true" selected="true">=== Select Districts ===</option>');

      $.each(data, function(index, districtsObj){
        $('#districts').append('<option value="'+ districtsObj.id +'">'+ districtsObj.name +'</option>');
      })
    });
  });

  $('#districts').on('change', function(e){
    // console.log(e);
    var districts_id = e.target.value;
    $.get('/json-village?districts_id=' + districts_id,function(data) {
      // console.log(data);
      $('#villages').empty();
      $('#villages').append('<option value="0" disable="true" selected="true">=== Select Villages ===</option>');

      $.each(data, function(index, villagesObj){
        $('#villages').append('<option value="'+ villagesObj.id +'">'+ villagesObj.name +'</option>');
      })
    });
  });


</script>

路线:


Route::get('/indonesia','CountryController@provinces');

Route::get('/json-regencies','CountryController@regencies');

Route::get('/json-districts', 'CountryController@districts');

Route::get('/json-village', 'CountryController@villages');

结果:


Please see the error here

2 个答案:

答案 0 :(得分:0)

请声明输入外观以在控制器中使用它。

use Illuminate\Support\Facades\Input;

请尝试一下,让我知道它如何工作:)

答案 1 :(得分:0)

如果要使用第一个选项,请确保已导入Input Facade:

use Illuminate\Support\Facades\Input;

public function regencies(Request $request){
  $provinces_id = $request->province_id;
  $regencies = Regencies::where('province_id', '=', $provinces_id)->get();
  return response()->json($regencies);
}

OR

导入HTTP请求并使用第二个选项:

use Illuminate\Http\Request;

public function regencies(Request $request){
  $provinces_id = $request->province_id;
  $regencies = Regencies::where('province_id', '=', $provinces_id)->get();
  return response()->json($regencies);
}