React中受控元件
阿新 • • 發佈:2022-01-16
import React, { Component } from 'react' export default class Input extends Component { constructor(props){ super(props); this.state={ username:"" } } render() { return ( <div> <form onSubmit={e=>this.handleSubmit(e)}> <label htmlFor="usrname"> 使用者:人,一定不能懶,懶習慣了,稍微努力一下便以為是在拼命。<input type="text" id="username" onChange={e=>this.handleChang(e)} value={this.state.value}/> </label> <input type="submit" value="提交" name="" id="" /> </form> </div> ) } handleSubmit(event){ event.preventDefault() } handleChang(event){ console.log(event.target.value);this.setState({ username:event.target.value }) } }