如何使用 ReactJS 通过 Material UI 对话框提交表单
How to submit the form by Material UI Dialog using ReactJS
我使用 Material UI 对话框制作了一个表单列表。官方案例:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
This dialog spans the entire width of the screen.
</Dialog>
操作是:
const actions = [
<FlatButton
label="Cancel"
primary={true}
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
primary={true}
onTouchTap={this.handleClose}
/>,
];
如何建一个表单让Dialog提交我的表单数据?
-------------------------------------------- - -更新 - - - - - - - - - - - - - - - - - - - - - - - -
还有一个答案:
在Dialog actions按钮中添加type
和form
的属性:
const actions = [
<FlatButton
label="Cancel"
primary={true}
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
primary={true}
onTouchTap={this.handleClose}
type="submit" //set the buttom type is submit
form="myform" //target the form which you want to sent
/>,
];
并在对话框中为表单提供属性 id:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
// here can put child component and the code still work even the form is in the child component
<div className="deal_form">
<form id="myform" onSubmit = {this.handleCreate} >
<TextField value={this.state.staff_number} />
</form>
</div>
</Dialog>
Dialog是materialui的一个ui组件,它不会自动提交你的表单数据,如果你想创建一个表单,在Dialog里面这样定义:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
/*CREATE THE FORM UI HERE*/
<div>Field1</div>
<div>Field2</div>
<div>Field3</div>
</Dialog>
如果表单包含多个字段,则使用对话框中的布尔值使内容可滚动autoScrollBodyContent = {true}
。
你定义了一个函数this.handleSubmit.bind(this)
点击提交按钮,在这个函数里面调用api提交你要提交的数据,一旦api调用成功,关闭对话框。
如果这解决了您的问题或您想要的任何其他详细信息,请发表评论。
您可以将
In HTML5 form=""
属性可用作对页面上任何表单的引用。所以按钮获得 form="my-form-id"
属性,表单获得 id="my-form-id"
属性。
return (
<Dialog
open
actions={[
<RaisedButton type="submit" form="my-form-id" label="Submit" />
]}
>
<form id="my-form-id" onSubmit={handleSubmit(this.onSubmit)}>
<TextField {...fields.username} floatingLabelText="Username" />
</form>
</Dialog>
);
我用的是MaterialUIv0.20.
我使用 Material UI 对话框制作了一个表单列表。官方案例:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
This dialog spans the entire width of the screen.
</Dialog>
操作是:
const actions = [
<FlatButton
label="Cancel"
primary={true}
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
primary={true}
onTouchTap={this.handleClose}
/>,
];
如何建一个表单让Dialog提交我的表单数据?
-------------------------------------------- - -更新 - - - - - - - - - - - - - - - - - - - - - - - -
还有一个答案:
在Dialog actions按钮中添加type
和form
的属性:
const actions = [
<FlatButton
label="Cancel"
primary={true}
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
primary={true}
onTouchTap={this.handleClose}
type="submit" //set the buttom type is submit
form="myform" //target the form which you want to sent
/>,
];
并在对话框中为表单提供属性 id:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
// here can put child component and the code still work even the form is in the child component
<div className="deal_form">
<form id="myform" onSubmit = {this.handleCreate} >
<TextField value={this.state.staff_number} />
</form>
</div>
</Dialog>
Dialog是materialui的一个ui组件,它不会自动提交你的表单数据,如果你想创建一个表单,在Dialog里面这样定义:
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
open={this.state.open}
>
/*CREATE THE FORM UI HERE*/
<div>Field1</div>
<div>Field2</div>
<div>Field3</div>
</Dialog>
如果表单包含多个字段,则使用对话框中的布尔值使内容可滚动autoScrollBodyContent = {true}
。
你定义了一个函数this.handleSubmit.bind(this)
点击提交按钮,在这个函数里面调用api提交你要提交的数据,一旦api调用成功,关闭对话框。
如果这解决了您的问题或您想要的任何其他详细信息,请发表评论。
您可以将
In HTML5 form=""
属性可用作对页面上任何表单的引用。所以按钮获得 form="my-form-id"
属性,表单获得 id="my-form-id"
属性。
return (
<Dialog
open
actions={[
<RaisedButton type="submit" form="my-form-id" label="Submit" />
]}
>
<form id="my-form-id" onSubmit={handleSubmit(this.onSubmit)}>
<TextField {...fields.username} floatingLabelText="Username" />
</form>
</Dialog>
);
我用的是MaterialUIv0.20.