在我的 web.php
Route::get('/show', 'StudentsController@show');
Route::POST('/student_delete/{id}', 'StudentsController@delete');
在我的控制器 StudentsController.php 中:
public function show()
{
$students = DB::table('students')
->select('*')
->get();
return view('students',['students'=>$students]);
}
public function delete($id)
{
DB::table('students')->where('id', '=', $id)->delete();
return back();
}
在我看來 student.blade.php :
<table>
<tr>
<th> name</th>
<th> tel</th>
<th> # </th>
</tr>
@for($m=0; $m< 10;$m )
<tr>
<td><input type="text" value="{{$students[$m]->name}}"/></td>
<td><input type="text" value="{{$students[$m]->tel}}"/></td>
<form action="/student_delete/{{$students[$m]->id}}" id="{{$students[$m]->id}}"method="POST">
{{ csrf_field() }}
<td><button type="submit" class="btn btn-danger remove-tr">-</button></td>
</form>
</tr>
@endfor
</table>
問題出在我的腳本中,在洗掉學生后觸發甜蜜??警報,如何獲取要洗掉的學生表格的 ID?這是我的腳本:
<script>
$("#How_to_get_the_id_of_form_here").submit(function(e) {
e.preventDefault();
var form = this;
Swal.fire({
title: 'do you want to continue?',
icon: 'question',
iconHtml: '?',
confirmButtonText: 'yes',
cancelButtonText: 'no',
showCancelButton: true,
showCloseButton: true
}).then((result) => {
if (result.isConfirmed) {
form.submit();
} else if(!result.isDenied) {
console.log(result.isDenied);
}
});
});
</script>
uj5u.com熱心網友回復:
我會以這種方式解決這個問題:將腳本包裝到函式中(id - 將作為函式引數)
function deleteStudent (id) {
e.preventDefault();
var form = $("#" id);
Swal.fire({
title: 'do you want to continue?',
icon: 'question',
iconHtml: '?',
confirmButtonText: 'yes',
cancelButtonText: 'no',
showCancelButton: true,
showCloseButton: true
}).then((result) => {
if (result.isConfirmed) {
form.submit();
} else if(!result.isDenied) {
console.log(result.isDenied);
}
});
};
然后將此函式設定為 onclick="deleteStudent ({{$students[$m]->id}});"
這里:
<form action="/student_delete/{{$students[$m]->id}}" id="{{$students[$m]->id}}"method="POST">
{{ csrf_field() }}
<td><button type="submit" class="btn btn-danger remove-tr" onclick="deleteStudent({{$students[$m]->id}});return false;">-</button></td>
</form>
或者 - 如果確認不起作用 - 唯一的方法 - 將腳本放入回圈中:
<table>
<tr>
<th> name</th>
<th> tel</th>
<th> # </th>
</tr>
@for($m=0; $m< 10;$m )
<tr>
<td><input type="text" value="{{$students[$m]->name}}"/></td>
<td><input type="text" value="{{$students[$m]->tel}}"/></td>
<form action="/student_delete/{{$students[$m]->id}}" id="{{$students[$m]->id}}"method="POST">
{{ csrf_field() }}
<td>
<button type="submit" class="btn btn-danger remove-tr">-</button>
</td>
</form>
<script>
$("#{{$students[$m]->id}}").submit(function(e) {
e.preventDefault();
var form = this;
Swal.fire({
title: 'do you want to continue?',
icon: 'question',
iconHtml: '?',
confirmButtonText: 'yes',
cancelButtonText: 'no',
showCancelButton: true,
showCloseButton: true
}).then((result) => {
if (result.isConfirmed) {
form.submit();
} else if(!result.isDenied) {
console.log(result.isDenied);
}
});
});
</script>
</tr>
@endfor
</table>
uj5u.com熱心網友回復:
當您可以使用所有表單共享的類時,為什么要專門使用 id ?
@for($m=0; $m< 10;$m )
...
<form class="student-form-delete" action="/student_delete/{{$students[$m]->id}}" id="{{$students[$m]->id}}"method="POST">
...
</form>
...
@endfor
并在您的 javascript 中使用該類名
<script>
$(".student-form-delete").submit(function(e) {
e.preventDefault();
var form = this;
Swal.fire({
...
});
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334623.html
標籤:javascript 查询
上一篇:貓頭鷹旋轉木馬導航效果
下一篇:來自表列的資料中不需要的前導空格
