简体   繁体   English

在ajax调用中使用jQuery刷新div

[英]Refresh div with jquery in ajax call

I have a div that has foreach's in them like so: 我有一个div,其中包含foreach,如下所示:

<div id="conversation">
   <?php foreach($singles as $question): ?>
     <div class="well well-sm">
       <h4><?php echo $question['question_title']; ?></h4>
     </div>
     <div class="bubble bubble--alt">
       <?php echo $question['question_text']; ?>
     </div>
   <?php endforeach; ?>
   <?php foreach($information as $answer): ?>
     <div class="bubble">
       <?php echo $answer['answer_text']; ?>
     </div>
   <?php endforeach; ?>
 </div>

And I also have a form to put in a new answer: 我也有一个表格可以输入新的答案:

<form method="post" style="padding-bottom:15px;" id="answerForm">
    <input type="hidden" id="user_id" value="<?php echo $_SESSION['user_id']; ?>" name="user_id" />
    <input type="hidden" id="question_id" value="<?php echo $_GET['id']; ?>" name="question_id" />
    <div class="row">
      <div class="col-lg-10">
        <textarea class="form-control" name="answer" id="answer" placeholder="<?php if($_SESSION['loggedIn'] != 'true'): ?>You must be logged in to answer a question <?php else: ?>Place your answer here <?php endif; ?>" placeholder="Place your answer here" <?php if($_SESSION['loggedIn'] != 'true'): ?>disabled <?php endif; ?>></textarea>
      </div>
      <div class="col-lg-2">
        <?php if($_SESSION['loggedIn'] != 'true'): ?>

        <?php else: ?>
          <input type="submit" value="Send" id="newAnswer" class="btn btn-primary btn-block" style="height:58px;" />
        <?php endif; ?>
      </div>
    </div>
  </form>

I am submitting the form via ajax and would like the div #conversation to refresh and reload the for each every time the user submits an answer to the question. 我正在通过ajax提交表单,并希望div #conversation每次用户提交问题答案时刷新并重新加载。 Right now I have the following ajax code: 现在,我有以下ajax代码:

<script type="text/javascript">
  $("#newAnswer").click(function() {
    var answer = $("#answer").val(); 
    if(answer == ''){
      $.growl({ title: "Success!", message: "You must enter an answer before sending!" });
      return false;
    }
    var user_id = $("input#user_id").val();
    var question_id = $("input#question_id").val();
    var dataString = 'answer='+ answer + '&user_id=' + user_id + '&question_id=' + question_id;
    $.ajax({  
      type: "POST",  
      url: "config/accountActions.php?action=newanswer",  
      data: dataString,  
      success: function() {  
         $.growl({ title: "Success!", message: "Your answer was submitted successfully!" });
         $("#answerForm").find("input[type=text], textarea").val("");
         $("#conversation").hide().html(data).fadeIn('fast');
      }  
    }); 
    return false;  
  });

</script>

You will notice that I have tried $("#conversation").hide().html(data).fadeIn('fast'); 您会注意到我已经尝试过$("#conversation").hide().html(data).fadeIn('fast'); but it did not successfully do the job. 但它没有成功完成这项工作。 It only reloaded the information that was passed through ajax into the div instead of just reloading the foreach. 它仅将通过ajax传递的信息重新加载到div中,而不是仅重新加载foreach。

How can I refresh the div or the <?php foreach(); ?> 如何刷新div或<?php foreach(); ?> <?php foreach(); ?> in the success function of the ajax call? <?php foreach(); ?>在成功函数中调用ajax?

Mitch, I'm looking at this part: 米奇,我在看这部分:

  success: function() {  
     $.growl({ title: "Success!", message: "Your answer was submitted successfully!" });
     $("#answerForm").find("input[type=text], textarea").val("");
     $("#conversation").hide().html(data).fadeIn('fast');
  } 

See the expression ".html(data)"??? 参见表达式“ .html(数据)” ??? Where is "data" being declared? 在哪里声明“数据”? The code above will never work. 上面的代码将永远无法工作。 Now, look at the lines below. 现在,看下面几行。 Particularly the first one. 特别是第一个。 See my change? 看到我的零钱吗?

  success: function(data) {  
     $.growl({ title: "Success!", message: "Your answer was submitted successfully!" });
     $("#answerForm").find("input[type=text], textarea").val("");
     $("#conversation").hide().html(data).fadeIn('fast');
  } 

Once you make this change, you need to use a debugger (chrome's or otherwise) to examine that what's coming back from your ajax call (which we don't have here) is what you need. 做出更改后,您需要使用调试器(chrome或其他工具)来检查ajax调用返回的内容(我们这里没有)。 But first, fix the bug. 但首先,修复该错误。

Good luck. 祝好运。

jQuery .load() method ( http://api.jquery.com/load/ ) can fetch and update single block from webpage. jQuery .load()方法( http://api.jquery.com/load/ )可以从网页中获取和更新单个块。 It will reload whole webpage in background, so some overhead is generated.. 它将在后台重新加载整个网页,因此会产生一些开销。

Change your ajax success to something like below: 将您的ajax成功更改为如下所示:

success: function() {  
  $.growl({ title: "Success!", message: "Your answer was submitted successfully!" });

  $("#conversation").load("config/accountActions.php #conversation >*");
}

This should load your conversation block and all it's childs and replace current(old) conversation block. 这应该加载您的对话块及其所有子项,并替换当前(旧)对话块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM