Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - HTML按钮不提交表单(HTML button to NOT submit form)

I have a form.

(我有一个表格。)

Outside that form, I have a button.

(在该表格之外,我有一个按钮。)

A simple button, like this:

(一个简单的按钮,如下所示:)

<button>My Button</button>

Nevertheless, when I click it, it submits the form.

(不过,当我单击它时,它将提交表单。)

Here's the code:

(这是代码:)

<form id="myform">
    <input />
</form>
<button>My Button</button>

All this button should do is some JavaScript.

(该按钮应该做的就是编写一些JavaScript。)

But even when it looks just like in the code above, it submits the form.

(但是,即使看起来像上面的代码一样,它也会提交表单。)

When I change the tag button to span, it works perfectly.

(当我将标签按钮更改为跨度时,它可以正常工作。)

But unfortunately, it needs to be a button.

(但不幸的是,它必须是一个按钮。)

Is there any way to block that button from submitting the form?

(有什么方法可以阻止该按钮提交表单?)

Like eg

(像例如)

<button onclick="document.getElementById('myform').doNotSubmit();">My Button</button>
  ask by arik translate from so

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

1 Reply

0 votes
by (71.8m points)

I think this is the most annoying little peculiarity of HTML... That button needs to be of type "button" in order to not submit.

(我认为这是HTML最令人讨厌的小特性...该按钮必须为“按钮”类型才能不提交。)

<button type="button">My Button</button>

Update 5-Feb-2019: As per the HTML Living Standard (and also HTML 5 specification ):

(更新2019年2月5日:根据HTML生活标准 (以及HTML 5规范 ):)

The missing value default and invalid value default are the Submit Button state.

(缺少的默认值无效的默认值是“提交按钮”状态。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...