首页 > 其他 > 详细

React 获取input输入的内容

时间:2020-03-31 10:33:43      阅读:112      评论:0      收藏:0      [点我收藏+]

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)
    }
} 

  

 

React 获取input输入的内容

原文:https://www.cnblogs.com/tlfe/p/12603267.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!