【问题标题】:Laravel posting and comments , i cannot comment after i created a new post using ajaxLaravel 发布和评论,我使用 ajax 创建新帖子后无法评论
【发布时间】:2017-05-15 06:37:45
【问题描述】:

当我使用浏览器刷新页面时,一切正常,我仍然可以发表评论,也可以添加新帖子,但问题是当我创建新帖子(这意味着像 facebook 帖子这样的帖子)时,我无法再发表评论了在任何帖子上。这是我的主页:

<div class="All-Post-Display" id="PostRefresh">
		<div class="container">
		@foreach($latest as $late)
			<div class="row">
				<div class="col-md-5 col-md-offset-2">
					<div class="outside-pannel">
						<div class="person">
							<img src="/styleimage/facebook.jpg">
							<h2>{{$late->user->name}} {{$late->user->lastname}}</h2><br>
							<h4>{{$late->created_at->diffForHumans()}}</h4>
						</div>
						<div class="body-of-post">
							<p>{{$late->body}}</p>
						</div>
						<div class="like-comment-share">
							<form>
								<ul>
									<li><a href="#"><i class="fa fa-thumbs-up"></i>Like</a></li>
									<li><a href="#"><i class="fa fa-comments-o"></i>Comment</a></li>
									<li><a href="#"><i class="fa fa-share"></i>Share</a></li>
								</ul>
							</form>
						</div>
						<div class="comment-section" >
							<div class="total-likes">
									<a href=""><h3>3 likes your post</h3></a>
							</div>
							<div class="comment-form">
							@foreach($late->comments as $comment)
								<div class="display-comments">
									<div class="comment-img">
										<img src="/styleimage/facebook.jpg">
									</div>
									<div class="comment-container">
										<div class="comment-content">
											<a name="postername" href="#">rd</a> <span></span>
											<div class="reply-like">
												<ul>
													<li><a href="">Like</a></li>
													<li><a href="">Reply</a></li>
													<li><h6>just now</h6></li>
												</ul>
											</div>
										</div>
									</div>
								</div>
							@endforeach
								<div>
									<div class="commenting-container">
									<img src="/styleimage/facebook.jpg">
									<textarea rows="1" id="comment-body{{$late->id}}" name="body" placeholder="Write a comment..."></textarea>
									<input type="text" name="commentable" id="posting-id{{$late->id}}" value="{{$late->id}}" style="display: none;">
										<div class="emojis">
											<ul>
												<li><a href="#"><i class="fa fa-smile-o"></i></a></li>
												<li><a href="#"><i class="fa fa-camera-retro"></i></a></li>
												<li><a href="#"><i class="fa fa-bookmark"></i></a></li>
											</ul>
										</div>
									</div>
										
									
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<script>
				$( document ).ready(function() {
				    $('#comment-body{{$late->id}}').keypress(function (e) {
				    	if (e.which == 13) {
				    	    var comment = $('#comment-body{{$late->id}}').val();
				    	    var postCommentedId = $('#posting-id{{$late->id}}').val();
				    	    $.ajax({
				    	    		url: '/comment',
				    	    		type: 'post',
				    	    		data: {'commentable_id':postCommentedId,'body':comment,'_token':$('input[name=_token]').val()},
				    	    		success: function (data) {
				    	    			console.log('success');
				    	    			$('#comment-body{{$late->id}}').val(null);

				    	    		},
				    	    		error: function(XMLHttpRequest, textStatus, errorThrown) {
				    	    		     console.log('errorThrown');
				    	    		  }
				    	    });
				    	}
				      	
				    });
				});
			</script>
		@endforeach
		</div>
	</div>
</div>
{{csrf_field()}}
<script>
$( document ).ready(function() {
     $('#ButtonPost').click(function (event) {
     	var thebody = $('#bodyPost').val();
     	$.ajax({
     			url: 'index',
     			type: 'post',
     			data: {'body':thebody,'_token':$('input[name=_token]').val()},
     			success: function (data) {
     				$('#PostRefresh').load(location.href + ' #PostRefresh');
     				$('#bodyPost').val(null);
     			}
     		});
     });
 	});


</script>

在底部运行此 AJAX 后,我的 @foreach 中的 AJAX 不起作用,当我发布新帖子时,id="PostRefresh" 将被刷新。我还使用 id="{{id of post}}" 使它们在每个帖子中都是独一无二的,请帮助谢谢...

【问题讨论】:

  • 注意:我的评论还没有完成,但我可以将它保存到数据库中,这样我就知道它是否有效
  • 不清楚你在问什么;这里与facebook 的联系到底是什么?
  • 我目前正在使用 laravel 复制 facebook 发帖。我可以在哪里登录发帖并发表评论,我的问题是每当我发帖时它会使评论不起作用,我现在可以做的是我需要重新加载整个页面才能再次发表评论跨度>
  • 什么,您是否通过他们的 API 使用 Facebook 的实际内容?或者你的意思是你想以与 Facebook 相同的方式实现某些东西?那么请不要标记它facebook,这不是这个标记的用途。
  • 哦,对不起,新手在这里我已经删除了标签,不是我只是从头开始复制它,它就像一个待办事项列表,每个列表都有评论。

标签: php jquery ajax laravel-5.4


【解决方案1】:

不是这个

$('#ButtonPost').click(function (event) ... 

尝试使用

$(document).on('click','#ButtonPost',(function (event) ...

这里的问题是,一旦你创建了新的 DOM,那么你没有为你的 jquery id 创建的新元素的 id 不起作用。 但是使用此代码,您将获得链接到 if #buttonPost 的每个元素的 jquery 代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2021-02-20
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多