提交Jquery表单后滚动到结尾

时间:2016-06-03 17:04:16

标签: jquery

我有一个基于jQuery Form Plugin的聊天。下面的脚本允许我在页面打开时向下滚动到最新消息,刷新聊天正文以查看新消息,并在发送消息时处理输入字段(id = 11)。

我想在发送消息并重新加载聊天正文后向下滚动到最新消息。我在#myForm的“成功”功能中尝试了这个,但它不起作用。

window.onload = function() {
  document.getElementById('scroll').scrollTop = 9999999999;
}

$(document).ready(function() {

  refreshChat();

});

function refreshChat() {

  $('#scroll').load('getchat.php', function() {
    setTimeout(refreshChat, 1000);
  });
}


var options = {
  clearForm: true,
  resetForm: true,
  beforeSubmit: function() {
    $('#11').val('sending...');
    $('#11').attr('disabled', true);
  },
  success: function() {
    $('#11').val('');
    $('#11').attr('disabled', false);
    $('#scroll').scrollTop(9999999999); // Part I'm trying to work on
  }

};

$('#myForm').ajaxForm(options);
<form id="myForm" name="message" enctype="multipart/form-data" method="post" action="sendmessage.php">
  <div class="row">
    <div class="large-12 columns">
      <div class="row collapse">
        <div class="large-11 medium-10 small-9 columns">
          <input type="text" name="message" id="11" class="chat-message radius" placeholder="Type your message here...">
        </div>
        <div class="large-1 medium-2 small-3 columns">
          <input type="submit" class="button postfix radius" value="Send">
        </div>
      </div>
    </div>
  </div>
</form>

1 个答案:

答案 0 :(得分:0)

我建议在这个jquery bot中编写一个函数 https://github.com/syaifulsz/simple-jquery-chatbot

这里是每条消息后调用的函数

function scrollToMessage() {
    var msgBox = $('#the name of the box');
    var height = msgBox[0].scrollHeight;
    msgBox.scrollTop(height);
}