获取已经发布的post字段并使用jquery更新它们
getting already posted post fields and updating them using jquery
我正在开发laravel,想编辑有多个字段要编辑的帖子。我已经用我的帖子的身体领域成功地做到了这一点,想用其他两个来实现
相关controller
代码:
$post->body=$data['body'];
$post->state = $data['state'];
$post->problem_domain = $data['problem_domain'];
$post->update();
return response()->json(['new_body'=>$post->body],500);
blade
需要从中提取早期数据:
<article class="post" data-postid="{{ $post->id }}">
<p> {{ $post->body }} </p>
<p> <b> Region: </b> {{ $post->state }}   <b> Problem Domain: </b>
{{ $post->problem_domain}}</p>
<div class="info">
posted by <b>{{ $post->user->name }}</b> on
<b>{{ $post->created_at }}</b>
</div>
<div class="interaction">
<a href="#" class="like">{{ Auth::user()->likes()->where('post_id',$post->id)->first() ? Auth::user()->likes()->where('post_id',$post->id)->first()->like == 1 ? 'You like this post':'Like':'Like'}} </a>
<a href="#" class="like">{{ Auth::user()->likes()->where('post_id',$post->id)->first() ? Auth::user()->likes()->where('post_id',$post->id)->first()->like == 0 ? 'You don''t like this post':'Dislike':'Dislike'}}</a>
@if(Auth::user() == $post->user)
<a href="#" class="edit" >Edit</a>
最后是我的js file
,请看看body
的格式是正确的,并希望对其他两个也这样做
var postId=0;
var postBodyElement=null;
$('.post').find('.interaction').find('.edit').on('click',function(event){
event.preventDefault();
postBodyElement=event.target.parentNode.parentNode.childNodes[1];
var postBody=postBodyElement.textContent;
postId=event.target.parentNode.parentNode.dataset['postid'];
$('#post-body').val(postBody);
$('#edit-modal').modal();
});
$('#modal-save').on('click',function()
{
$.ajax(
{
method : 'POST',
url : urlEdit,
data: {body: $('#post-body').val(), state: $('#post-state').val(),
problem_domain: $('#post-problem_domain').val(), postId: postId, _token: token}
})
.done(function(msg)
{
$(postBodyElement).text(msg['new_body']);
$('#edit-modal').modal('hide');
});
}
);
在您的控制器上,您只返回以下行的主体:
return response()->json(['new_body'=>$post->body],500);
您应该返回完整的模型或其他属性,例如:
return response()->json($post, 500);
以及在您的刀片中更新其他属性。
相关文章:
- jQuery-更新jQuery.ajax().done()中点击的图像
- 基于复选框 Jquery 更新文本框
- 根据复选框选择 jQuery 更新文本框
- JQuery:更新表内容花费的时间太长
- 如何使用Jquery更新数据确认属性中的消息
- Jquery更新到1.10.2后,Jquery Ajax Json对象未显示
- jQuery更新DOM并运行change()方法
- 使用jQuery更新选择的多个字段的数量
- 当原始内容通过jQuery更新时,我如何告诉PageDown更新其预览
- jQuery更新文本字段不起作用
- 有没有更好的方法可以用jquery更新图像的高度和宽度
- 如何使用jquery更新表coulmn
- 使用jQuery更新的Kendo UI网格值不会更新传递回来的模型
- 使用jQuery更新CSS属性
- PrimeFaces 命令按钮使用 jQuery 更新数据表内部
- 创建 Jquery 更新函数
- jquery 更新嵌套对象的键
- 如何使用 jQuery 更新表
- Drupal - Jquery 更新版本与灯箱冲突
- 使用 AJAX Jquery 更新 JSON