【发布时间】:2019-08-19 04:37:04
【问题描述】:
我的 Laravel 视图有问题。我正在通过 ajax 获得所需的数据,我也可以在我的视图中显示该数据。但是,我发现每当我在下拉列表中选择某些内容时,数据就会出现,但整体布局会重复。我看到我的控制台并发现在表格中我在使用下拉列表选择某些内容后创建了另一个布局。我有我的代码,请检查一下。
<select>`enter code here`
@foreach(App\StudentsClass::all() as $class)
<option id="class{{$class->id}}" value="{{$class->id}}">{{$class->class_name}}</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>
<a class="fas fa-folder-open btn btn-success float-right mb-4 mr-2"> Save</a>
</div>
<script>
$(document).ready(function () {
@foreach(App\StudentsClass::all() as $class)
$("#class{{$class->id}}").click(function () {
var classes = $("#class{{$class->id}}").val();
$.ajax({
url: '{{url('/students/attendance')}}',
type: "GET",
dataType: "html",
data: 'class_id=' + classes,
success:function(response) {
// console.log(response);
$('table[id=studentsData]').html(response);
}
});
});
@endforeach
});
【问题讨论】:
-
您能否添加一个示例,说明您从
/students/attendanceurl 返回的数据? -
我从数据 url 获取的数据是学生行 ID、学生 ID 和学生姓名,但是在成功获取这些数据后,我每次都看到我的布局被重复并且有些奇怪。我在控制台中看到我的表格中有整个布局,但在我的代码中我没有类似的东西
-
我注意到的另一件事是,如果我删除了那个 url,那么我的代码仍然可以正常工作
-
我真的不知道幕后发生了什么
标签: javascript jquery laravel laravel-blade