react中怎么获取input的值


当前第2页 返回上一页

通过event对象信息的方式

1

2

3

4

5

6

7

8

9

10

11

<input onChange={(e)=>this.inputChange(e)}/>

<button onClick={()=>this.getInputValue} >获取input的值</button>

inputChange(e){

alert(e.target.value)

this.setState({

username:e.target.value

})

}

getInputValue(){

alert(this.state.username)

}

使用ref的方式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<input ref='username' onChange={()=>this.inputChange()}/>

<button onClick={()=>this.getInputValue()} >获取input的值</button>

inputChange(){

//获取dom节点元素

//1.添加ref属性

//2.使用this.refs.username获取dom节点

let val=this.refs.username.value;

this.setState({

username:val

})

}

getInputValue(){

console.log(this.state.username)

}

使用ref自定义一个属性,可以通过this.refs.属性名称.value获取内容。

以上就是react中怎么获取input的值的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

React 生命周期函数有哪些

vue和React能做什么

React中怎么传递事件对象

React怎么显示隐藏

使用React怎么遍历数组

React怎么添加css样式

怎么在React代码中打断点

vue和React中dom的区别

React怎么mock数据

React遵循的原则是什么

更多相关阅读请进入《React》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...