[英]Dynamic dependent select option using AJAX and Laravel 5.8
I am actually trying to obtain a dynamic dependent select option for city based on region and in my controller i am returning a string(using echo) but unfortunately the string is not rendering in the select option(of cities) on the browser. 我实际上试图基于区域获取城市的动态依赖选择选项,并且在我的控制器中我返回一个字符串(使用echo),但是不幸的是,该字符串未在浏览器的(城市)选择选项中呈现。 but i can see it by inspection or by printing on the console. 但我可以通过检查或在控制台上打印看到它。
my jquery: 我的jQuery:
<script >
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val() != '')
{
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('dynamicdependent.fetch') }}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#cityName').html(result);
console.log(result);
console.log(result);
}
})
}
});
$('#RegionName').change(function(){
$('#cityName').val('');
});
});
</script>
: my controller :我的控制器
class DynamicDependent extends Controller
{
public function fetch(Request $request)
{
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = city::where($select,$value)->get();
$output = '<option value="">Select '.ucfirst($dependent).'</option>';
foreach($data as $row)
{
$output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
}
echo $output;
}
}
:no eroor message :没有错误的讯息
I suspect that the response might not be that well formed. 我怀疑答案可能不是那么好。
Try to return a proper response instead of echoing yourself. 尝试返回正确的响应,而不是回声自己。
// other code in the controller ...
return response()->json($output);
If it doesn't help, please include the actual response you get in the browser/console. 如果没有帮助,请在浏览器/控制台中包含您得到的实际响应。
thanks. 谢谢。 actually i was using bootstrap select library and there is a class called selectpicker when i removed this class from the select tag of city it worked fine 实际上我在使用引导选择库,当我从城市的选择标签中删除此类时,有一个名为selectpicker的类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.