我正在为我的搜索项目的国家/地区城市实现动态下拉选择。我在运行代码方面遇到困难。我只会得到错误。
我有一些用于Controller,Java和HTML代码的代码。从数据库加载国家(地区)工作正常,但不会加载一个国家拥有的城市。
型号
namespace App;
use App\Listings;
use Illuminate\Database\Eloquent\Model;
class Cities extends Model
{
protected $table = 'newcitymun';
protected $fillable = ['citymunDesc'];
public $timestamps = false;
public static function getCitymunInfo($id)
{
return Citymun::find($id);
}
}
控制器
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
protected $table = 'location';
protected $fillable = ['location_name'];
public $timestamps = false;
}
索引
<select id="location" name="location" class="form-control">
<option value="">{{ __('messages.select_location') }}</option>
@foreach(\App\Location::orderBy('location_name')->get() as $location)
<option value="{{$location->id}}">{{$location->location_name}}</option>
@endforeach
</select>
<select id="citymun" name="citymun" class="form-control">
<option value="">{{ __('messages.select_location') }}</option>
@foreach(\App\Citymun::orderBy('citymunDesc')->get() as $citymun)
<option value="{{$citymun->id}}">{{$citymun->citymunDesc}}</option>
@endforeach
</select>
JavaScript
<script type="text/javascript">
$(document).ready(function(e) {
$("#location").change(function(){
var loc=$("#location").val();
$.ajax({
type: "GET",
url: "{{ URL::to('ajax_citymun') }}/"+loc,
success: function(result){
$("#citymun").html(result);
}
});
});
});
</script>
citymun php
<option value="">.. select city ..</option>
@foreach($citymun as $i => $citymun)
<option value="{{$citymun->id}}">{{$citymun->citymunDesc}}</option>
@endforeach