Javascript 每个循环的Ajax附加按钮Laravel 5.4
我正在尝试在Javascript 每个循环的Ajax附加按钮Laravel 5.4,javascript,php,jquery,ajax,laravel-5.4,Javascript,Php,Jquery,Ajax,Laravel 5.4,我正在尝试在foreach循环中创建一个“添加按钮”。并使用ajax提交。但是我很难找出我到底做错了什么,我的JS知识很差 这是我的代码 视图: @foreach ($lots as $lot) @if (count($lot->comments) >= 1) <span class="glyphicon glyphicon-ok"></span> @else <form id="favori
foreach
循环中创建一个“添加按钮”。并使用ajax提交。但是我很难找出我到底做错了什么,我的JS知识很差
这是我的代码
视图:
@foreach ($lots as $lot)
@if (count($lot->comments) >= 1)
<span class="glyphicon glyphicon-ok"></span>
@else
<form id="favorites-button" data-id="{{ $lot->lot_id }}">
{{ csrf_field() }}
<input type="hidden" name="body" value="0_o" class="form-control">
<button type="submit" class="btn btn-xs btn-primary">Favorites</button>
</form>
@endif
@endforeach
和我的控制器:
public function add(Lot $lot)
{
Comment::create([
'body' => request('body'),
'lot_id' => $lot->lot_id,
'user_id' => auth()->user()->id,
'bid' => $lot->bid,
'lot_date' => $lot->lot_date,
'auction_name' => $lot->auction_name,
'pics_urls' => $lot->pics_urls,
'company' => $lot->company,
'model_name_en' => $lot->model_name_en,
'model_type_en' => $lot->model_type_en,
'scores_en' => $lot->scores_en,
'model_year_en' => $lot->model_year_en,
'color_en' => $lot->color_en,
'displacement' => $lot->displacement,
'transmission_en' => $lot->transmission_en,
'start_price_en' => $lot->start_price_en,
'inspection_en' => $lot->inspection_en,
'grade_en' => $lot->grade_en,
'equipment_en' => $lot->equipment_en,
'mileage_num' => $lot->mileage_num,
'result_en' => $lot->result_en,
'auct_ref' => $lot->auct_ref,
]);
return "ok";
}
所以我认为问题也出在我在foreach
循环中使用的ID
中。这意味着循环中的所有表单都具有相同的ID。所以我需要重新考虑我的整个方法。有人知道我该怎么做吗?
$(“文档”).ready(函数(){
$(“#收藏夹按钮”).submit(函数(e){
e、 预防默认值();
var按钮=$(此按钮);
var url='/lots/'+button.data('id')+'/comment';
var body=$(“输入[name=body]”).val();
$.ajax({
类型:“POST”,
url:url,
数据:{
身体:身体,,
_令牌:“{csrf_token()}}”
},
数据类型:“json”,
成功:功能(数据){
console.log('success');
}
}“json”);
});
});
所以一开始我做错了一切
我改变了整个方法。感谢我的朋友们的帮助
现在,首先,我将表单更改为一个按钮,该按钮调用一个事件:
这是一种观点:
@foreach ($lots as $lot)
@if (count($lot->comments) >= 1)
<span class="glyphicon glyphicon-ok"></span>
@else
<button data-id="{{ $lot->lot_id }}" class="btn btn-xs btn-primary favouriteButton">Favourite</button>
@endif
@endforeach
并创建了新功能:
public function favouriteButton(Lot $lot){
Comment::create([
'body' => 'Auto add', // this is the body for new comment
'lot_id' => $lot->lot_id,
'user_id' => auth()->user()->id,
'bid' => $lot->bid,
'lot_date' => $lot->lot_date,
'auction_name' => $lot->auction_name,
'pics_urls' => $lot->pics_urls,
'company' => $lot->company,
'model_name_en' => $lot->model_name_en,
'model_type_en' => $lot->model_type_en,
'scores_en' => $lot->scores_en,
'model_year_en' => $lot->model_year_en,
'color_en' => $lot->color_en,
'displacement' => $lot->displacement,
'transmission_en' => $lot->transmission_en,
'start_price_en' => $lot->start_price_en,
'inspection_en' => $lot->inspection_en,
'grade_en' => $lot->grade_en,
'equipment_en' => $lot->equipment_en,
'mileage_num' => $lot->mileage_num,
'result_en' => $lot->result_en,
'auct_ref' => $lot->auct_ref,
]);
return "ok";
}
我希望我的经验能对一些人有所帮助 将类用于表单而不是ID,您很高兴了解您,它解决了我的一些问题。还有什么问题?它只使用带有如下主体的令牌
?\u token=lmgdmciowqt9mgv76rw6vvvxgaqjewze9wrhwjjqu&body=0\o
@foreach ($lots as $lot)
@if (count($lot->comments) >= 1)
<span class="glyphicon glyphicon-ok"></span>
@else
<button data-id="{{ $lot->lot_id }}" class="btn btn-xs btn-primary favouriteButton">Favourite</button>
@endif
@endforeach
<!-- Ajax add to favorites -->
<script type="text/javascript">
// call button via class
$('.favouriteButton').click(function (e) {
var button = $(this);
e.preventDefault();
// change the route
$.get('/lots/' + $(this).data('id') + '/favourite')
.done(function (response) {
// instead of button insert check icon
button.parent().append('<span class="glyphicon glyphicon-ok"></span>');
// remove button
button.remove();
})
.fail(function (respnse) {
});
});
</script>
Route::get('/lots/{lot}/favourite','CommentsController@favouriteButton');
public function favouriteButton(Lot $lot){
Comment::create([
'body' => 'Auto add', // this is the body for new comment
'lot_id' => $lot->lot_id,
'user_id' => auth()->user()->id,
'bid' => $lot->bid,
'lot_date' => $lot->lot_date,
'auction_name' => $lot->auction_name,
'pics_urls' => $lot->pics_urls,
'company' => $lot->company,
'model_name_en' => $lot->model_name_en,
'model_type_en' => $lot->model_type_en,
'scores_en' => $lot->scores_en,
'model_year_en' => $lot->model_year_en,
'color_en' => $lot->color_en,
'displacement' => $lot->displacement,
'transmission_en' => $lot->transmission_en,
'start_price_en' => $lot->start_price_en,
'inspection_en' => $lot->inspection_en,
'grade_en' => $lot->grade_en,
'equipment_en' => $lot->equipment_en,
'mileage_num' => $lot->mileage_num,
'result_en' => $lot->result_en,
'auct_ref' => $lot->auct_ref,
]);
return "ok";
}