網(wǎng)站首頁 編程語言 正文
說明:借助于 ref,使用原生DOM方式來獲取表單元素的值
ref的作用:獲取DOM 或組件
步驟
1 創(chuàng)建ref對象
?? ?構(gòu)造方法里:
?? ?this.txt = React.createRef()
2 將創(chuàng)建好的ref對象添加到文本框中
??? ?<input type='text' ref={this.txt}/>
3 通過ref獲取到文本框的值
?? ?this.txt.current.value
備注: 這種方式知道就行,大多數(shù)情況還是使用表單的 受控組件來處理
代碼
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
constructor() {
super()
//創(chuàng)建ref
this.txt = React.createRef()
}
fn=()=>{
alert(this.txt.current.value);
}
render() {
return (
<div>
{/*將創(chuàng)建好的ref對象添加到文本框中*/}
<input type='text' ref={this.txt}/>
<button onClick={this.fn}>獲取文本框的值</button>
</div>
)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));
效果
?
原文鏈接:https://blog.csdn.net/m0_45877477/article/details/125811456
相關(guān)推薦
- 2023-02-27 詳解C++?STL模擬實現(xiàn)list_C 語言
- 2023-07-14 react 利用antd-mobile實現(xiàn)樓層效果
- 2023-04-11 Python中字符串類型代碼的執(zhí)行函數(shù)——eval()、exec()和compile()詳解_pyt
- 2022-04-04 使用uniapp封裝簡易通用的ajax請求以及攜帶token去請求 (代碼優(yōu)化新增請求日志和響應日志
- 2022-05-25 Windows系統(tǒng)修改Jenkins端口號_win服務器
- 2023-06-03 C#利用后綴表達式解析計算字符串公式_C#教程
- 2021-11-15 Linux網(wǎng)絡設置之基礎操作命令詳解_Linux
- 2022-12-27 教你使用Psycopg2連接openGauss的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支