duanre4421 2012-08-16 18:55
浏览 89
已采纳

如何在表单提交上停止页面刷新

I have a form which automatically refreshes the page on submit, I have tried adding:

onSubmit="return false;" to the form element, but then the form just stops and nothing happens when you click submit.

I wouldnt mind the page refresh so much but the form is at the bottom of the page and the refresh kicks you back to the top. So I tried this approach:

<form name='test' method='POST' action="index.php" onSubmit="window.scrollTo(5000,500);">

This works for a split second but then something else overrides it (not sure what)

I have also tried using php: header.location just to get a "headers have already been sent" error.

The site in question can be seen here, and the form is at the very bottom.

The only two jquery libraries I am using that I could foresee any conflicts with are nicescroll and (more likely) waypoints, but i dug through them both and couldn't find any conflicting issues.

If anyone knows of a way to keep the functionality of the form but stop the refresh of the page, that would be wonderful Thanks

EDIT: After reading the answers below, it looks like I will have to use ajax to acomplish this, I have absolutely no experience with ajax, so I will see how that goes.

  • 写回答

3条回答 默认 最新

  • dqp10099 2012-08-16 18:57
    关注

    It seems you need to go through of way of AJAX submission in that case. In that case, you can use jQuery $.ajax() method to do that. A sample below:

    HTML

    <form name='test' method='POST' action="index.php">
    

    jQuery

    $('form[name=test]').submit(function(e) {
       e.preventDefault();
       window.scrollTo(5000,500);
    
       // a sample AJAX request
       $.ajax({
         url : this.action,
         type : this.method,
         data : $(this).serialize(),
         success : function(response) {
    
         }
       });
    });
    

    Here, .preventDefault() is for stop page refresh on form submit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大