我可以做一个<按钮>不提交表格?

2022-01-30 00:00:00 jquery javascript html jquery-ui htmlbutton

我有一个表单,有 2 个按钮

I've got a form, with 2 buttons

<a href="index.html"><button>Cancel changes</button></a>

<button type="submit">Submit</button>

我也使用jQuery UI的按钮,就像这样

I use jQuery UI's button on them too, simply like this

$('button').button();

但是,第一个按钮也会提交表单.我会认为如果它没有 type="submit",它就不会.

However, the first button also submits the form. I would have thought that if it didn't have the type="submit", it wouldn't.

显然我可以做到这一点

$('button[type!=submit]').click(function(event) { event.stopPropagation(); });

但是有没有一种方法可以阻止后退按钮在没有 JavaScript 干预的情况下提交表单?

But is there a way I can stop that back button from submitting the form without JavaScript intervention?

说实话,我只使用了一个按钮,所以我可以使用 jQuery UI 来设置它的样式.我尝试在链接上调用 button() 并没有按预期工作(看起来很丑!).

To be honest, I used a button only so I could style it with jQuery UI. I tried calling button() on the link and it didn't work as expected (looked quite ugly!).

推荐答案

button 元素的type 属性 是提交".将其设置为 type="button" 以生成不提交表单的按钮.

The default value for the type attribute of button elements is "submit". Set it to type="button" to produce a button that doesn't submit the form.

<button type="button">Submit</button>

用 HTML 标准:什么都不做."

相关文章