Onclick不应该触发表单数据提交

问题描述:

我刚刚发现,每当 onclick 事件发生时,我的<按钮> 放置在< form> 标记触发器,表单提交它的数据,就好像我点击了< input type ='submit'>
我不想那样。我的表单中的按钮用于执行其他任务,表单在我点击其中一个后不应提交数据。

I just found out that every time onclick event for my <button> placed inside <form> tag triggers, form submits it's data as if i clicked <input type='submit'>. I don't want that. Buttons inside my form serve other task, form shouldn't submit data after i clicked one of them.

为了更清楚,我想要这个代码:

To be more clear, i want this code:

<form action="http://www.google.com" method="POST">
    <button onclick="alert('hi!')">Button</button>
    <br>
    <input type="submit" value="submit"/>
</form>

显示警报hi!当我点击按钮时,它不应该打开谷歌。它只应显示谷歌,当我按提交。

to show alert "hi!" when i click on the Button and it shouldn't open Google after that. It should only show Google when i press "submit".

指定 type =button

Specify type="button":

<button type="button" onclick="alert('hi!')">Button</button>

来自链接的文章:

From the linked article:


如果未指定属性,则此[提交]为默认值。