• react 中获取多个input输入框中的值的 俩种写法


    目录

    1. 使用受控组件

    2. 使用非受控组件


    1. 使用受控组件

    这是React中最常见的方法,每个输入框都与React组件的state相关联,并通过onChange事件来更新state。

    代码示例:

    1. import React, { Component } from 'react';
    2. class MultipleInputExample extends Component {
    3. constructor(props) {
    4. super(props);
    5. this.state = {
    6. input1: '',
    7. input2: '',
    8. input3: ''
    9. };
    10. }
    11. handleInputChange = (event) => {
    12. const name = event.target.name;
    13. const value = event.target.value;
    14. this.setState({
    15. [name]: value
    16. });
    17. }
    18. handleSubmit = (event) => {
    19. event.preventDefault();
    20. const { input1, input2, input3 } = this.state;
    21. // 现在你可以在这里使用 input1、input2 和 input3 的值
    22. console.log('Input 1:', input1);
    23. console.log('Input 2:', input2);
    24. console.log('Input 3:', input3);
    25. }
    26. render() {
    27. return (
    28. <form onSubmit={this.handleSubmit}>
    29. <input
    30. type="text"
    31. name="input1"
    32. value={this.state.input1}
    33. onChange={this.handleInputChange}
    34. />
    35. <input
    36. type="text"
    37. name="input2"
    38. value={this.state.input2}
    39. onChange={this.handleInputChange}
    40. />
    41. <input
    42. type="text"
    43. name="input3"
    44. value={this.state.input3}
    45. onChange={this.handleInputChange}
    46. />
    47. <button type="submit">提交button>
    48. form>
    49. );
    50. }
    51. }
    52. export default MultipleInputExample;

    2. 使用非受控组件

    在这种方法中,你可以使用ref来获取输入框的值。这通常在需要与非受控库或DOM集成时使用。

    代码示例:

    1. import React, { Component } from 'react';
    2. class MultipleInputExample extends Component {
    3. constructor(props) {
    4. super(props);
    5. this.inputRefs = {
    6. input1: React.createRef(),
    7. input2: React.createRef(),
    8. input3: React.createRef()
    9. };
    10. }
    11. handleSubmit = () => {
    12. const input1Value = this.inputRefs.input1.current.value;
    13. const input2Value = this.inputRefs.input2.current.value;
    14. const input3Value = this.inputRefs.input3.current.value;
    15. console.log('Input 1:', input1Value);
    16. console.log('Input 2:', input2Value);
    17. console.log('Input 3:', input3Value);
    18. }
    19. render() {
    20. return (
    21. <div>
    22. <input type="text" ref={this.inputRefs.input1} />
    23. <input type="text" ref={this.inputRefs.input2} />
    24. <input type="text" ref={this.inputRefs.input3} />
    25. <button onClick={this.handleSubmit}>提交button>
    26. div>
    27. );
    28. }
    29. }
    30. export default MultipleInputExample;

  • 相关阅读:
    设计模式单例模式
    vue3+ts+uniapp实现小程序端input获取焦点计算上推页面距离
    【QT】QT自定义C++类
    视频上传阿里云,如何获取视频的某一帧作为封面??
    windows10系统:C++高性能 Web 服务开发框架oat++环境配置
    使用凌鲨进行聚合搜索
    ftp端口号20和21的区别是什么?
    03UE4 C++ 入门【世界坐标系与局部坐标系】
    【洛谷 P3853】[TJOI2007] 路标设置 题解(二分答案+循环)
    京东到家MySQL容器化,为何首选Docker而非K8S?
  • 原文地址:https://blog.csdn.net/XikYu/article/details/133941087