【问题标题】:is it possible to get multiple input from the input field which is created dynamically in react js?是否可以从在 react js 中动态创建的输入字段中获取多个输入?
【发布时间】:2022-01-08 06:23:26
【问题描述】:

我正在创建一个带有表单的网页,其中我将根据从后端获得的值动态创建一个输入字段。我已经创建了输入字段,但是当我单击添加按钮时,我不知道如何从每个字段中检索数据并将它们发送到后端,我知道我可以使用静态的目标 ID 获取输入,但是在这种形式下,每个 id 都是动态的。有没有什么概念可以获取输入!

这是我生成动态输入字段的代码。

    class UserInput extends React.Component {
  render() {
    const repinput = (event) => {
      this.setState({
        [event.target.id]: event.target.value,
      });
      console.log({ [event.target.id]: event.target.value });
    };

    return (
      <div className="cell">
        <div className="callout">
          {this.props.example.map((item, i) => (
            <div key={i}>
              <div className="grid-x">
                <div className="cell medium-2">
                  <div className="grid-x">
                    <div className="cell large-12">
                      <label className="labelalignment">
                        <h3> {item.BECname} : </h3>
                      </label>
                    </div>
                  </div>
                </div>

                <div className="cell medium-6">
                  <div className="grid-x">
                    <div className="cell large-12">
                      <label>
                        <input
                          type="text"
                          id={item.BECid}
                          placeholder="10.2"
                          onChange={repinput}
                          required
                        />
                      </label>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          ))}

          <div className="cell medium-12">
            <div className="grid-x">
              <div className="cell large-6">
                <div className=" flex-box">
                  <input className="styled" type="button" value="Add Data" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

这是我的输出

【问题讨论】:

    标签: reactjs json forms react-hooks react-web


    【解决方案1】:

    试试这个,我已经更新了你的代码

    class UserInput extends React.Component {
      repinput = (event) => {
        this.setState({
          [event.target.name]: event.target.value
        });
      };
    
      onSubmit = () => {
        console.log(this.state);
      };
    
      render() {
        return (
          <div className="cell">
            <div className="callout">
              {this.props.example.map((item, i) => (
                <div key={i}>
                  <div className="grid-x">
                    <div className="cell medium-2">
                      <div className="grid-x">
                        <div className="cell large-12">
                          <label className="labelalignment">
                            <h3> {item.BECname} : </h3>
                          </label>
                        </div>
                      </div>
                    </div>
    
                    <div className="cell medium-6">
                      <div className="grid-x">
                        <div className="cell large-12">
                          <label>
                            <input
                              type="text"
                              id={item.BECid}
                              placeholder="10.2"
                              onChange={this.repinput}
                              name={item.BECname}
                              required
                            />
                          </label>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              ))}
    
              <div className="cell medium-12">
                <div className="grid-x">
                  <div className="cell large-6">
                    <div className=" flex-box">
                      <input
                        className="styled"
                        type="button"
                        value="Add Data"
                        onClick={this.onSubmit}
                      />
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        );
      }
    }
    

    【讨论】:

    • 谢谢,兄弟,这是一件我不知道的简单事情,我认为它不会因为动态 id 而工作,所以我只想使用添加按钮上的 id 检索数据点击。
    • 我想你也是在上一篇文章中帮助我的人,非常感谢你!!! :)
    • 哈哈,乐于帮助@selvasurya :)
    • 我需要你的帮助,当我将 this.state 值发送到后端 PHP 文件时,我在邮递员中收到了一个错误的字符串,有什么办法可以解决这个问题,{3: '33' , 7: '44', 9: '55'} 这是我在控制台中获得的值,并通过附加使用 Axios 发送到 PHP。这里的概念是我需要将值放在两个差异中。一个列 id 中的第 9 列和另一列中的第 55 列值反之亦然在三行中@Vishnu Sajeev
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    相关资源
    最近更新 更多