Can I make a lt;buttongt; not submit a form?(我可以做一个lt;按钮gt;不提交表格?)
问题描述
我有一个表单,有 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 标准:什么都不做."
这篇关于我可以做一个<按钮>不提交表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以做一个<按钮>不提交表格?


基础教程推荐
- 动态更新多个选择框 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01