react 中文文档案例六 (表单)
Posted lolita-web
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react 中文文档案例六 (表单)相关的知识,希望对你有一定的参考价值。
class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoing: true, numberOfGuests: 2, value: ‘‘ }; this.handleInputChange = this.handleInputChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleChange = this.handleChange.bind(this); } handleInputChange(event) { const target = event.target; const value = target.type === ‘checkbox‘ ? target.checked : target.value; const name = target.name; this.setState({ [name]: value }); } handleChange(event) { this.setState({value: event.target.value}); } handleSubmit(event) { alert(‘A name was submitted: ‘ + this.state.value); event.preventDefault(); } render() { return ( <form onSubmit={this.handleSubmit}> <label> Is going: <input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} /> </label> <br /> <label> Number of guests: <input name="numberOfGuests" type="number" value={this.state.numberOfGuests} onChange={this.handleInputChange} /> </label> <br /> <label> Name : <input type="text" value={this.state.value} onChange={this.handleChange} /> </label> <input type="submit" value="Submit" /> </form> ); } } ReactDOM.render( <Reservation />, document.getElementById(‘root‘) );
以上是关于react 中文文档案例六 (表单)的主要内容,如果未能解决你的问题,请参考以下文章