Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

javascript - 单击窗体中的按钮时阻止刷新页面(prevent refresh of page when button inside form clicked)

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.

(它确实如此,但是有了不必要的结果,它刷新了页面。)

My simple code goes like this

(我的简单代码是这样的)

<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?

(我该怎么做才能阻止页面刷新?)

  ask by bunkdeath translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add type="button" to the button.

(在按钮上添加type =“button”。)

<button name="data" type="button" onclick="getData()">Click</button>

The default value of "type" for a button is "submit", which self posts the form in your case and makes it look like a refresh.

(按钮的“type”的默认值是“submit”,它会自动在您的情况下发布表单并使其看起来像刷新。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...