我通过ajax以JSON格式从数据库中获取了一些数据。我可以在ajax代码中使用一些HTML代码显示数据,但这不是我想要的。我想借助一些ID将这些数据引用到我上面的实际表中...有可能还是有人有想法?
下面是我的代码
//我的代码
<option value="">--- Select State ---</option>
@foreach ($classes as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
<table id="studentsData" class="table table-striped table-bordered table-list-search">
<thead>
<tr>
<th>#</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Attendance</th>
</tr>
</thead>
@foreach($students as $student)
<tbody>
<tr>
<th>{{$student->id}}</th>
<td>{{$student->student_id}}</td>
<td>{{$student->first_name}} {{$student->last_name}}</td>
<td>
<div class="form-group">
<select class="form-control" id="gender">
<option>Present</option>
<option>Absent</option>
<option>Leave</option>
</select>
</div>
</td>
</tr>
</tbody>
@endforeach
</table>
//我的javascrip
$(document).ready(function() {
$('select[name="students_class_id"]').on('change', function() {
var classID = $(this).val();
if(classID) {
$.ajax({
url: '/myform/ajax/'+classID,
type: "GET",
dataType: "json",
success:function(data) {
// $('#studentsData').html(response);
$('table[id="studentsData"]').empty();
$.each(data, function(key, value) {
var markup = '<tr> <td>' + value.id + '</td> <td>' + value.student_id + '</td> <td>' + value.first_name+ ' ' + value.last_name + '</td> <tr>';
$('table[id="studentsData"]').append(markup);
});
}
});
}
});
});