Onclick不应该触发表单数据提交 [英] Onclick shouldn't trigger form data submission

查看:208
本文介绍了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:


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

这篇关于Onclick不应该触发表单数据提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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