Laravel、jquery和select列出了完整的代码
直到最后一行代码,我无法将其加载到视图的Select列表中,我都没有问题。我无法获得第二个人口稠密国家、地区和城市的选择列表,但我很累,因为我觉得这一切都很清楚,我不明白为什么不能。在firebug中,我可以在HTML选项卡上加载区域选择列表,但无法将其发送到视图。您可能认为这只是将数组从控制器发送到视图的问题,但这不会起作用,因为它会抱怨视图中有一个未定义的变量。因此,如果以ajax方式预生成了任何值,那么您不能让任何变量在那里等待任何值,因此我也可以在控制器或模型中生成它,但它只能在Firebug中查看。我将非常感激,因为我已经到了我能力的极限。我让它在Codeigniter中工作,但无法在Laravel中复制它。以下是材料: JQUERY代码Laravel、jquery和select列出了完整的代码,jquery,laravel,Jquery,Laravel,直到最后一行代码,我无法将其加载到视图的Select列表中,我都没有问题。我无法获得第二个人口稠密国家、地区和城市的选择列表,但我很累,因为我觉得这一切都很清楚,我不明白为什么不能。在firebug中,我可以在HTML选项卡上加载区域选择列表,但无法将其发送到视图。您可能认为这只是将数组从控制器发送到视图的问题,但这不会起作用,因为它会抱怨视图中有一个未定义的变量。因此,如果以ajax方式预生成了任何值,那么您不能让任何变量在那里等待任何值,因此我也可以在控制器或模型中生成它,但它只能在Fire
<script type="text/javascript">
jQuery(document).ready(function(){
//When the first select list changes, then the function activates itself
$('#country').change(cargarProvincias); //load regions when countries change
});
function cargarProvincias() {
// collects the selected value
var id_pais = $('#country').val();
// sends the parameter to the controller. It is definitively a correct url as I can see the result
$regionsurl = '{{URL::route('getregionslist')}}';
//I tried also with $.post, same results as with get. Had to change the route accordingly, of course.
$.get( $regionsurl, {'id_pais':id_pais} ) ,function(resp){
//then the controller gets that value and sends it to the Model
// and the Model does retrieve the correct result.
// I put that result into a string and that should be picked up by the
// html function here below (inside the resp parameter) and fill up the select lists at the View
$('#regions').empty().html(resp);
};
}
</script>
这是控制器:
public function getregionslist(){
$id_pais = Input::get('id_pais');
(new region)->getregion($id_pais);
}
It does get the correct id from the View and also gets (if I try it, the correct feedback from the Model)
HERE IS THE MODEL (it does get correct results) but it does not get echoed at the View
public function getregion($id_pais) {
$regions = DB::table('regions')->where('id_pais', $id_pais)->get();
$cadena = "";
foreach ($regions as $region){
$cadena .= "<option value ='$region->id_region'>$region->nombre_region</option>";
}
echo $cadena;
return $cadena;
}
例如,如果我在Firebug中看到cadena为加拿大带来了什么,它会带来这个,这是正确的:
<option value ='2055'>Alberta</option><option value ='2056'>British Columbia</option><option value ='2053'>Manitoba</option><option value ='2050'>New Brunswick</option><option value ='2047'>Newfoundland</option><option value ='2058'>Northwest Territories</option><option value ='2048'>Nova Scotia</option><option value ='2057'>Nunavut</option><option value ='2052'>Ontario</option><option value ='2049'>Prince Edward Island</option><option value ='2051'>Quebec</option><option value ='2054'>Saskatchewan</option><option value ='2059'>Yukon Territory</option>
这种观点是:
私人土地
<select name ="regions" id ="regions" class="form-control">
</select>
是的,我们希望我们可以从控制器发送数组,然后在视图中执行@foreach$regions->$region,但这将失败,因为它总是返回未定义的变量$regions,因此,我必须在模型或控制器上执行此操作,而不是让任何变量在视图中等待值。而不是使用以下代码:
$id_pais = Input::get('id_pais');
(new region)->getregion($id_pais);
在控制器中使用以下代码:
$region = Region::whereId(Input::get('id_pais'))->lists('id_region', 'nombre_region');
return Response::json($region);
然后在客户端使用jQuery构建下拉列表/选择,如下所示:
$.getJSON($regionsurl, {'id_pais':id_pais}, function(resp) {
$('#regions').empty();
$.each(resp, function(key, value){
var option = $('<option/>', {'id':key, 'text':value});
$('#regions').append(option);
});
});
class Region Extends Eloquent {
protected $table = 'regions';
protected $primaryKey = 'id_pais'; // if id_pais is your primary key
// code...
}
p/S:你需要从中学习/阅读更多。非常感谢你的解决方案,我投了赞成票。除此之外,我找到了问题的原因。这是一个错误地放在jquery函数中的括号。单词function左边的括号是罪魁祸首id_pais':id_pais},function很高兴你做到了,+1:-是的,我花了大约24小时,但是你的解决方案非常有趣,我从中学习到了。getJson也应该是getJson,这是一个打字错误。