React 获取input输入的内容

react为我们提供了两种获取value的方法

第一种:非受控方法获取

import React,{Component} from 'react'
export default class system extends Component{
    constructor(props){
        super(props)
        this.state = {
            acc:''
        }
    }
    render(){
        return (
            <div>
                <input ref="ss"></input>
                <button onClick={this.aaaa.bind(this)}>提交</button>
            </div>
        )
    }
    aaaa(){
        console.log(this.refs.ss.value)
    }
} 

第二种:受控方式获取

import React,{Component} from 'react'
export default class system extends Component{
    constructor(props){
        super(props)
        this.state = {
            acc:''
        }
    }
    render(){
        return (
            <div>
                <input value={this.state.acc} onChange={this.cccc.bind(this)}></input>
                <button onClick={this.aaaa.bind(this)}>提交</button>
            </div>
        )
    }
    cccc(e){
        this.setState({
            acc:e.target.value
        })
    }
    aaaa(){
        console.log(this.state.acc)
    }
} 

  

猜你喜欢

转载自www.cnblogs.com/tlfe/p/12603267.html
今日推荐