Javascript 在引导模式中重新加载div后使用scrollTop时,不考虑新高度

Javascript 在引导模式中重新加载div后使用scrollTop时,不考虑新高度,javascript,jquery,ajax,twitter-bootstrap,Javascript,Jquery,Ajax,Twitter Bootstrap,我有一个基于引导的模式,允许用户添加和查看帖子: <div class="modal fade hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="prodCommentModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> &

我有一个基于引导的模式,允许用户添加和查看帖子:

<div class="modal fade hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="prodCommentModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <div class="modal-title">Comments</div>
      </div>

      <div class="modal-body" data-no-turbolink>
        <span id="modal-body-span">
          <%= render partial: 'posts/getposts' %>
        </span>
      </div>

      <div class="modal-footer">
        <%= form_for(@micropost) do |f| %>
          <div class="field" id="post-box">
            <%= f.text_area :content, placeholder: "Write a post..." %>
          </div>
          <div class="post-submit">
            <%= f.submit "Post", id: "post_button", class: "btn btn-primary" %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>
模式是在聊天风格中完成的,最新的帖子被添加到底部。我有单独的javascript,当模态第一次打开时,它会自动将模态主体区域滚动到底部。但我无法工作的是,在提交一篇新文章后(当模态仍然打开时),滚动到底部。我注意到了一些事情:

  • 如果在加载前后打印$('.modal body').prop(“scrollHeight”),则该值不变
  • 如果我向上滚动帖子,然后使用一个较大的值,比如$('.modal body').scrollTop(10000),那么它在提交后会向下滚动,但会滚动到上一篇帖子的底部

scrollheight似乎会在以后更新,在更新之前,scrollTop最多只能滚动到旧值。有什么想法吗?

经过多次尝试,终于找到了有效的方法(ajaxComplete事件)。我还需要解决的问题是,自从上次重新加载页面/模式以来,可能已经添加了新的帖子,我希望在模式中包含最新和最棒的内容

$(document).ready(function() {
    $('#post_button').click(function (e) {
        e.preventDefault();

        $.post('/posts',
            $('#new_post').serialize(),
            function (data, status, xhr) {
                $('.modal-body').load(document.URL+' #modal-body-span');
                $('#post_content').val('');
            });
    });

    $(document).on('show.bs.modal', '.modal', function () {
        $('.modal-body').load(document.URL+' #modal-body-span');
    });

    $(document).ajaxComplete(function() {
        $('.modal-body').scrollTop($('.modal-body').prop("scrollHeight"));
    });

    $('#myModal').on('shown.bs,modal', function () {
        $('.modal-body').scrollTop($('.modal-body').prop("scrollHeight"));
    });
});    
如果有更好或更优雅的解决方案,我还是很乐意听到

$(document).ready(function() {
    $('#post_button').click(function (e) {
        e.preventDefault();

        $.post('/posts',
            $('#new_post').serialize(),
            function (data, status, xhr) {
                $('.modal-body').load(document.URL+' #modal-body-span');
                $('#post_content').val('');
            });
    });

    $(document).on('show.bs.modal', '.modal', function () {
        $('.modal-body').load(document.URL+' #modal-body-span');
    });

    $(document).ajaxComplete(function() {
        $('.modal-body').scrollTop($('.modal-body').prop("scrollHeight"));
    });

    $('#myModal').on('shown.bs,modal', function () {
        $('.modal-body').scrollTop($('.modal-body').prop("scrollHeight"));
    });
});