react怎么改变input的value

怎么使用react改变Input的value


export default class myForm extends Component{

    constructor(props){
        super(props);
        this.state = {
            id: '',
        };
    }
    
    change=()=>{
        var idInput = this.refs.idInput;
        ***// TODO 怎么改变input的值***
    }

    render(){
        return(
            <Input
               ref="idInput"
               placeholder="请输入"
               onChange={e => this.setState({id:e.target.value})}
            />
            <Button onClick={this.change} />
        )
    }
}
阅读 3.1k
2 个回答
export default class myForm extends Component{

    constructor(props){
        super(props);
        this.state = {
            id: '',
        };
    }
    
    change=()=>{
        this.setState({id: 新的id}) 
    }

    render(){
        return(
            <Input
               ref="idInput"
               value={this.state.id}
               placeholder="请输入"
               onChange={e => this.setState({id:e.target.value})}
            />
            <Button onClick={this.change} />
        )
    }
}

findDOMNode(this.refs.idInput)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题