我可以做一个<按钮>不提交表格? [英] Can I make a <button> not submit a form?

查看:34
本文介绍了我可以做一个<按钮>不提交表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,有 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 标准:什么都不做."

这篇关于我可以做一个&lt;按钮&gt;不提交表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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