我尝试在laravel 5.5中使用X-editable,但无法使其工作。到目前为止,我有这个控制器:
public function updatecommentajax(Request $request)
{
return Rating::find($id)->update([
'comment' => $request->get('comment'),
]);
}
这是我的路线:
Route::post('/updatecommentajax/{id}', 'RatingController@updatecommentajax')->name('updatecommentajax');
这是我的刀锋:
<form action="{{route('updatecommentajax', $rating->id)}}" method="post">
{{csrf_field()}}
<td class="text-center">
<a href="#" id="comment" class="comment" data-url="{{ route('updatecommentajax', $rating->id) }}" data-pk="{{ $rating->id }}" data-type="textarea" data-placement="right" data-title="Edit Comment">{{$rating->comment}}</a>
</td>
</form>
我的JS代码:
<script type="text/javascript">
$(document).ready(function() {
//toggle `popup` / `inline` mode
$.fn.editable.defaults.mode = 'popup';
rating_id = $(this).data('pk');
url = $(this).data('url');
//make username editable
$('#comment').editable({
url: url,
pk: rating_id,
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type:"POST",
validate:function(value){
if($.trim(value) === '')
{
return 'This field is required';
}
}
});
});
</script>
任何人都知道这是什么问题?
PS:不知何故只会选择第一条评论进行编辑而其他评论将被选中(猜测是因为
id="comment"
但是如果我将其更改为{{$rating->id}}
我以后不知道如何在此部分中获取该评论$('#comment').editable({
我设法让它发挥作用但我上面的PS
中的问题仍然存在。这是我的修正码:
刀片:
<form action="{{route('updatecommentajax', $rating->id)}}" method="post">
{{csrf_field()}}
<td class="text-center">
<label for="comment" hidden></label>
<a href="#" id="comment" class="comment" data-url="{{ route('updatecommentajax', $rating->id) }}" data-pk="{{ $rating->id }}" data-type="textarea" data-placement="right" data-title="Edit Comment">{{$rating->comment}}</a>
</td>
</form>
脚本:
<script type="text/javascript">
$(document).ready(function() {
//toggle `popup` / `inline` mode
$.fn.editable.defaults.mode = 'inline';
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
rating_id = $(this).data('pk');
url = $(this).data('url');
//make username editable
$('#comment').editable({
url: url,
pk: rating_id,
type:"textarea",
validate:function(value){
if($.trim(value) === '')
{
return 'This field is required';
}
}
});
$('#rating').editable({
url: url,
pk: rating_id,
type:"select",
source: [
{value: 1, text: '1 Star'},
{value: 2, text: '2 Star'},
{value: 3, text: '3 Star'},
{value: 4, text: '4 Star'},
{value: 5, text: '5 Star'}
],
validate:function(value){
if($.trim(value) === '')
{
return 'This field is required';
}
}
});
});
</script>
答案 0 :(得分:0)
$('#comment').editable({
更改为$('.comment').editable({
ID更改为Class来修复它。希望对别人有所帮助。