【问题标题】:Refresh div after posting a comment发表评论后刷新div
【发布时间】:2014-08-27 18:06:25
【问题描述】:

我想添加一条评论并立即使用 ajax 在线程中显示它

这是我的代码:

JSP:

<div class="comments">
     //Here i show the list of comments
    <logic:iterate id="comment" name="discussion" property="comments">
        <div class='comment'>
            <div class='title'> ${comment.author.firstName}<br>
                ${comment.date}</div>
            <div class='commentContent'>
                ${comment.content}
            </div>              
        </div>
    </logic:iterate>
</div>

//this is my comment form
<html:form action="/sauvegarderCommentaire.do" styleId="formComment">
    <label>Ecrire un commentaire:</label>
    <br>
    <textarea name="comment"> </textarea>
    <input type="submit" name="submit" id="submitComment" />
</html:form>

jquery:

$("#submitComment").click(
     function() {
         $("#formComment").ajaxSubmit({
                 error : function() {
                         alert("Il y a une erreur AJAX");
                 },
                 success: function(e) {                               
                           //Here I try to reload only the comments part of my page
                          $(".comments").load("/myApp/discussion.do .comments");
                 }
         });            
});

希望我说清楚了?!! 否则我可以提供更多细节:)

【问题讨论】:

  • 您遇到的错误是什么?
  • 完全没有错误,只是没有显示刚刚发布的人的评论!

标签: jquery ajax refresh


【解决方案1】:

试试这个。

首先将 id 添加到您的文本区域。

<textarea name="comment" id="tar"></textarea>

用这个替换你的jquery代码。

$('#submitComment').click(function() {
var tar = $('#tar').val();
$.ajax({
    type : "POST",
    url : "/myApp/discussion.do", //url where u want to send the request
    data : { tar : tar },
    success : function(resp){
        $('.commentContent').html(resp);
    },
    error : function(resp){
        alert("Error AJAX");
    }        
});
$(".comments").load("/myApp/discussion.do .comments");  
});

【讨论】:

  • 不,它不起作用!我认为问题在于我没有得到新的 cmets 列表,因为当我刷新页面时会出现新评论
  • 你没有在你的 ajax 中提到类型和 URL。
  • 我认为 load() 将转到 servlet 重新加载讨论对象,因此我得到了新的 cmets 列表!好像不是这样的!??
  • 对不起最后的评论,实际上表单中提到了url和数据:action="/sauvegarderCommentaire.do",看看jsp部分当我在表单上调用ajaxSubmit时,它被提交了
  • 这几乎是一样的,你只是改变了语法!我正在寻找的是当我更新讨论对象(其中包含 cmets 列表)时,我想让它恢复更新,目前还没有发生
猜你喜欢
  • 2013-06-15
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2020-12-18
  • 2021-04-20
  • 2014-07-09
  • 1970-01-01
相关资源
最近更新 更多