防止页面内的按钮在点击时刷新页面

防止页面内的按钮在点击时刷新页面

问题描述:

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

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>