【react基础】7、react获取文本框的值
生活随笔
收集整理的這篇文章主要介紹了
【react基础】7、react获取文本框的值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
React獲取文本框的值
1、e.target.value
2、用ref標記節點,ref="textbox",使用時調用this.refs.textbox.value
3、用ref標記節點,ref={(node)=>this.txt=node,node表示當前節點(名稱可以修改),把當前節點賦值給txt。使用時調用this.txt.value
例子:
import React from 'react'class Counter extends React.Component {constructor(props){super(props);}input = (e)=>{if(e.keyCode===13){//console.log(e.target.value) //方法1//console.log(this.refs.textbox.value) //方法2console.log(this.txt.value) //方法3}}render(){return <div>{/* 方法2*/}{/* <input type="text" ref="textbox" onKeyUp={this.input} /> */}{/* 方法3 */}<input type="text" ref={(node)=>this.txt=node} onKeyUp={this.input} /></div>}}export default Counter;總結
以上是生活随笔為你收集整理的【react基础】7、react获取文本框的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ctf php正则截断,记[BJDCTF
- 下一篇: Qt支持C语言开发