react表单提交
Posted dkplus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react表单提交相关的知识,希望对你有一定的参考价值。
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'coconut'};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite La Croix flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<label>
<input value={this.state.value} onChange={this.handleChange}/>
</label>
<label>
<textarea value={this.state.value} onChange={this.handleChange}/>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
ReactDOM.render(
<FlavorForm />,
document.getElementById('root')
);
以上是关于react表单提交的主要内容,如果未能解决你的问题,请参考以下文章
在 react-testing-library 中按 Enter 提交表单不起作用