React - Preventing Form Submission(React - 阻止表单提交)
问题描述
我一直在尝试使用 React.在我的实验中,我使用的是 Reactstrap 框架.当我点击一个按钮时,我注意到 HTML 表单提交了.有没有办法在单击按钮时阻止表单提交?
I've been experimenting with React. In my experiement, I'm using the Reactstrap framework.When I click a button, I've noticed that the HTML form submits. Is there a way to prevent form submission when a button is clicked?
我在这里重新创建了我的问题.我的表单非常基本,如下所示:
I've recreated my issue here. My form is pretty basic and looks like this:
<Form>
<h3>Buttons</h3>
<p>
<Button color="primary" onClick={this.onTestClick}>primary</Button>
</p>
</Form>
我错过了什么?
推荐答案
我认为首先值得注意的是,如果没有 javascript(纯 html),则在单击 < 时会提交
或 form
元素;input type="submit" value="submit form"><button>也提交表单</button>
.在 javascript 中,您可以通过使用事件处理程序并在按钮单击或表单提交时调用 e.preventDefault()
来防止这种情况.e
是传递给事件处理程序的事件对象.使用 react,两个相关的事件处理程序可以通过表单作为 onSubmit
使用,另一个在按钮上通过 onClick
使用.
I think it's first worth noting that without javascript (plain html), the form
element submits when clicking either the <input type="submit" value="submit form">
or <button>submits form too</button>
. In javascript you can prevent that by using an event handler and calling e.preventDefault()
on button click, or form submit. e
is the event object passed into the event handler. With react, the two relevant event handlers are available via the form as onSubmit
, and the other on the button via onClick
.
示例:http://jsbin.com/vowuley/edit?html,js,控制台,输出
这篇关于React - 阻止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React - 阻止表单提交


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