使用ajax + laravel使用bootstrap模式更新帖子我遇到了json part

时间:2017-01-29 14:14:01

标签: json ajax twitter-bootstrap laravel

我试图用ajax更新帖子并且这样做,我使用bootstrap模式,我以前在填充我的模态时遇到了问题但我现在通过使用jquery来定位元素来解决它我现在遇到了问题保存和关闭模态我假设由.done部分引起的问题。任何帮助表示赞赏 这是我的代码用于编辑的js文件:

var postId = 0;
var postBodyElement = null;
var postTitleElement = null;

$('.post').find('.edit').on('click',function(event) {
event.preventDefault();

 var post = $(event.target).closest('.post');
 postTitleElement = post.find('.post-title').text();
 postBodyElement = post.find('.post-body').text();

 var postTitle = postTitleElement;
 var postBody = postBodyElement;

 postId = event.target.parentNode.parentNode.dataset['postid'];

 $('#post-title').val(postTitle);
 $('#post-body').val(postBody);

 $('#edit-modal').modal();
});

$('#modal-save').on('click', function() {
$.ajax({
    method: 'Post',
    url: urlEdit,
    data: {title: $('#post-title').val(), body: $('#post-body').val(),postId: postId, _token: token}
})
    .done(function (msg){

        $(postTitleElement).text(msg['new_title']);
        $(postBodyElement).text(msg['new_body']);
        $('#edit-modal').modal('hide');
    });
});

我预测

    $(postTitleElement).text(msg['new_title']);
    $(postBodyElement).text(msg['new_body']);

我遇到问题的地方而不是.text我必须做其他事情。 自从我开始编辑以来,我的所有bootstrap javascript组件都出现问题。

这是模式:

<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Edit the Article</h4>
            </div>
            <div class="modal-body">
                <form action="">
                    <div class="datasetpost form-group">
                        <label for="post-title">Title</label>

                        <input type="text" class="form-control" name="title" id="post-title">
                        <br>
                        <label for="post-body">Post Body</label>
                        <textarea class="form-control" name="post-body" id="post-body" cols="60" rows="10"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

这是他们出现的地方:

<div class="media-body">
                <header class="post-title"><h5 class="media-heading">{{ $post->title }}<i class="fa fa-angle-down pull-right " aria-hidden="true"></i></h5> </header>
                <p class="post-body"><i>{{ $post->body }}</i></p>
                <div class="info">
                    Posted by {{ $post->user->name }} on {{ $post->created_at }}
                </div>
              @if (Auth::check())
                <div class="interaction">
                      <a href="#" class="like">Like</a> |
                      <a href="#" class="like">Dislike</a>
                @if (Auth::user() == $post->user)
                        |
                      <a href="#" class="edit">Edit</a> |
                      <a href="{{ route('post.delete', ['post_id' => $post->id]) }}" class="like">Delete</a>
                 @endif
                </div>
              @else
                <a href="{{ route('registeration') }}" class="like" style="color: red">Login to interract with posts!</a>
              @endif
            </div>

我对解决方案的猜测将是.done()中的内容 因为我已经发送了.text,因为我在js文件中看到了,因为我有这些行:

 var post = $(event.target).closest('.post');
 postTitleElement = post.find('.post-title').text();
 postBodyElement = post.find('.post-body').text();

 var postTitle = postTitleElement;
 var postBody = postBodyElement;

 postId = event.target.parentNode.parentNode.dataset['postid'];

 $('#post-title').val(postTitle);
 $('#post-body').val(postBody);

例如postBodyTitle .text.post-title分配.text时结束.done() 但我再次尝试将其转换为<input type="hidden" name="updateid" value="<?php echo $id; ?>> if(isset($_POST['up-submit'])){ $id = $_POST['updateid']; //add this. 内的某些内容?而且乐趣开始了吗?

0 个答案:

没有答案