【问题标题】:How to stop Redirecting a page when a form is submitted?提交表单时如何停止重定向页面?
【发布时间】:2017-07-19 10:51:21
【问题描述】:

我正在使用一个表单,当表单被提交时,我使用了 action 属性并调用了一个服务器 url 来保存表单数据

这是它的外观:

<form name="example" action="webresources/data/update" method="post">
</form>

上述表单在提交时会将表单数据更新到服务器,但同时它也将我带到新页面webresources/data/update,它只有服务器响应

我不希望此回复显示在网页上。提交表单时应该调用 url,并且它应该在同一页面上而不重定向到新页面。

有什么办法吗,我只能通过表单参数发送数据。

提前谢谢你:)

【问题讨论】:

    标签: javascript html forms web-services restful-url


    【解决方案1】:

    去掉表单中的action属性,把按钮放到表单外。

    onclick 时,进行 ajax 调用。

    <form>
    stuff
    </form>
    <input type="button" onclick="ajax()">
    
    <script>
    function ajax(){}
    </script>
    

    【讨论】:

      【解决方案2】:
      <form name="example" action="webresources/data/update" method="post" onclick="return false">
      </form>
      

      返回 false 将停止页面重新加载或刷新。

      我建议在您提出问题之前先进行一次快速的 google 搜索,因为这是prevent form submit from redirecting/refreshing using javascript 为我找到的第一个结果。

      【讨论】:

        猜你喜欢
        • 2016-01-15
        • 2018-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-07
        • 1970-01-01
        • 2013-10-27
        相关资源
        最近更新 更多