我有这个ajax响应,我对如何将其转换为刀片模板内的foreach循环感到困惑。我想做的是获取一个月中的天数,然后为每个日期显示复选框。
控制器:
public function days(Request $request){
$days=array();
$id = $request->input('months');
$month = $id;
$year = 2018;
for($d=1; $d<=31; $d++) {
$time=mktime(12, 0, 0, $month, $d, $year);
if(date('m', $time)==$month && date('w', $time)>0 && date('w', $time)<6) {
$days[]=date('Y-m-d H:i:s', $time);}}
return response()->json($days);}
Ajax:
$(document).ready(function(){
$('.testing').on('change',function(){
var test=$(this).val();
$.ajax({
type:'get',
url:'/days',
data:{"months":test,"_token": "{{ csrf_token() }}"},
success: function(data){
$('#ajax_data').html(data);
}}); });});
刀片
<select class="testing" name="test" id="test">
<option value="0" dissable="true" selected="true">Select</option>
<option value="1" >JAN</option>
<option value="2">FEB</option>
</select>@foreach($days as $day)<input type="checkbox" name="days[]" > @endforeach
答案 0 :(得分:1)
就像@boussadjra一样,您应该删除foreach
<select class="testing" name="test" id="test">
<option value="0" dissable="true" selected="true">Select</option>
<option value="1" >JAN</option>
<option value="2">FEB</option>
<div id="days"></div>
$(document).ready(function(){
$('.testing').on('change',function(){
var test=$(this).val();
$.ajax({
type:'get',
url:'/days',
data:{"months":test,"_token": "{{ csrf_token() }}"},
success: function(data){
$("#days").empty();
$.each(data,function(index,value){
$("#days").append('<input type="checkbox" value="'+value+'" name="days[]" > '+value+'<br/>');
});
}
});
});
});