在表单提交后发布滚动位置以将用户返回到页面上的相同位置

时间:2015-10-02 13:35:33

标签: javascript forms scroll

<>我正在尝试更改输入的值,以便我可以在提交表单时将用户返回到页面上的相同位置

<form> 
 <input type ="text" name="scroll_pos" id ="vert" value="0" > 

</form>


</div>

<script type="text/javascript">

    $(window).scroll(function (event) {
    var vp = $(window).scrollTop();

  document.getElementById("vert").value = vp.toString();
});
抱歉,如果它是一个愚蠢的问题,但为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

尝试以下代码,

我还建议您使用 ajax ..因此,当表单提交时,您可以保持在同一页 而无需刷新

&#13;
&#13;
$(document).ready(function() {
  var position = $("#vert").val();
  $(window).scroll(function(event) {
    var scroll = $(window).scrollTop();
    $("#vert").attr('value',scroll);
    //$('input[name=scroll_pos]').val(scroll);
    console.log(scroll);
  });
  
  $(window).scrollTop(position);
});
&#13;
div {
  height: 500px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <form>
    <input type="text" name="scroll_pos" id="vert" value="0">

  </form>
</div>
&#13;
&#13;
&#13;

相关问题