這就是我所說的方法,在這裏我通過ID刪除事件:的Javascript的onclick:只能使用一次
function delete_event(id)
{
$.ajax({
url: "/delete/"+id,
type: "post",
data: { '_token': '{{csrf_token()}}' },
success: function(data) {
if (data.success == 1)
{
$("#alert").addClass("alert alert-success");
$("#alert").innerText = "it worked";
$("#alert").remove();
$("alert").fadeOut(3000);
}
else
{
$("#alert").addClass("alert alert-danger");
$("#alert").text("it didn't work");
$("#alert").delay(3000).fadeOut(1000);
}
},
error: function(data)
{
console.log("neieieiin");
console.log(data);
},
complete: function(){}
})
}
下面是HTML碼:
<div id="alert">
</div>
<table class="table table-striped">
<tr>
<th>Event Name</th>
<th>Starting Date</th>
<th>Location</th>
<th>Actions</th>
</tr>
@foreach($events as $event)
<tr id="{{$event->event_id}}">
<td>{{$event->event_name }}</td>
<td>{{$event->start_date}}</td>
<td>{{$event->street_address}}</td>
<td>
<a>
<button onclick="delete_event({{$event->event_id}})" type="button" class="btn btn-default">
<i class="fa fa-times" aria-hidden="true"></i>
</button>
</a>
</td>
</tr>
@endforeach
</table>
@endsection
當我點擊刪除按鈕只有一次顯示它工作/它沒有工作。如果我第二次點擊它,沒有任何建議?再次
<button data-id="{{$event->event_id}}" type="button" class="btn btn-default delete_button">
jQuery(document).on("click",".delete_button",function(){
var id = $(this).data("id");
$.ajax({
url: "/delete/"+id,
type: "post",
data: { '_token' : '{{csrf_token()}}' },
success: function(data) {
if(data.success == 1)
{
//do as you want
}
},
});
});
只需調試它,調試器,console.log。 – dfsq
謝謝,他進入delete_event,但它沒有進入ajax請求。有任何想法嗎? – AppleForTheKing