【发布时间】:2018-11-08 08:55:11
【问题描述】:
我想在反应中收听引导模式关闭事件,因为我在模式中有一个表单,并且想在模式关闭时清除字段。我知道如何在 jquery 中做到这一点,
$('#modal-form').on('hidden.bs.modal', fnClearForm);
但是这里我想在组件中绑定一个函数。
注意:我不能使用 react-bootstrap。 这是一个类似的question,但它没有解决我的问题。
这是我的组件,
class MyModal extends Component {
clearForm = () => {
-- code here --
}
render() {
return (
<div className="modal right fade" id="add-user-modal" role="dialog">
<div className="modal-dialog" role="document">
<div className="modal-content">
-- form goes here --
</div>
</div>
</div>
)
}
这是我打开模式的方式,
<a className="btn" data-toggle="modal" data-target="#add-user-modal">..</a>
【问题讨论】:
-
问题不清楚。您可以使用相同的方式执行此操作,但您可能会在 清除表单 的组件中使用
this.handleModalClose而不是fnClearForm。您开始在componentDidMount内监听该事件。 -
在您打开模态和模态关闭处理函数的位置分享您的代码
-
@SergiuParaschiv 请告诉我怎么做。我已经发布了我的组件代码
-
$('#modal-form').modal('hide');
标签: javascript reactjs bootstrap-modal