简体   繁体   English

提交表单后如何停止重定向页面?

[英]How to stop Redirecting a page when a form is submitted?

I'm using a form and when the form is submitted I used action attribute and called a server url where the form data gets saved 我使用的是表单,提交表单时,我使用了action属性,并调用了保存表单数据的服务器网址

Here's how it looks : 外观如下:

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

The above form when submitted updates the form data in to the server but at the same time it also takes me to the new page webresources/data/update which has nothing but server response 上面的表单在提交时会将表单数据更新到服务器中,但与此同时,它也将我带到了新页面webresources/data/update ,该页面仅包含服务器响应

I don't want this response to be shown on the web page. 我不希望此响应显示在网页上。 The url should be called when the form is submitted and it should be on the same page without redirecting to a new page. 提交表单时应调用url,并且该URL应位于同一页面上而不重定向到新页面。

Is there any way to do this, I'm allowed to send the data only via form parameters. 有没有办法做到这一点,我只能通过表单参数发送数据。

Thank you in advance :) 先感谢您 :)

Remove the action attribute in the form and put the button outside the form. 删除表单中的action属性,然后将按钮放在表单外部。

When onclick, make an ajax call. 当onclick时,进行ajax调用。

<form>
stuff
</form>
<input type="button" onclick="ajax()">

<script>
function ajax(){}
</script>
<form name="example" action="webresources/data/update" method="post" onclick="return false">
</form>

Returning false will stop the page from reloading or refreshing. 返回false将阻止页面重新加载或刷新。

I suggest that before you ask a question you do a quick google search because this was the first result that came up for me prevent form submit from redirecting/refreshing using javascript . 我建议您在问一个问题之前先进行一个快速的Google搜索,因为这是我想到的第一个阻止表单提交使用javascript重定向/刷新的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM