防止页面内的按钮在点击时刷新页面 [英] prevent refresh of page when button inside form clicked

查看:574
本文介绍了防止页面内的按钮在点击时刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用表单内的按钮时遇到问题。我想要那个按钮来调用函数。它确实存在,但有不必要的结果,它会刷新页面。

I have an issue while using buttons inside form. I want that button to call function. It does, but with unwanted result that it refresh the page.

我的简单代码如下所示

<form method="POST">
    <button name="data" onclick="getData()">Click</button>
</form>

点击按钮后,该函数会刷新页面,重置所有先前的请求当前页面是前一个请求的结果。

On clicking the button, the function gets called with page refreshed, which resets all my previous request which affects the current page which was result of the previous request.

我应该怎么做才能防止页面刷新?

What should I do to prevent the page refresh?

推荐答案

getData()返回false。这将解决它。

Let getData() return false. This will fix it.

<form method="POST">
    <button name="data" onclick="return getData()">Click</button>
</form>

这篇关于防止页面内的按钮在点击时刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆