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插件

React中ant design mobile是什么

谷歌怎么调试React项目

怎么下载React

React中如何引入插件

2021年前端React面试题大汇总(收藏)

React中如何引入样式

可以用什么编辑React native?

webpack 不识别React怎么办

使用React native的好处是什么?

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




打赏

取消

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

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

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

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

评论

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