当前第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(){
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》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » react中怎么获取input的值