【问题标题】:How to get the text field value and push that value to an arrayList如何获取文本字段值并将该值推送到 arrayList
【发布时间】:2018-02-17 21:49:51
【问题描述】:

如何获取文本字段值并将该值推送到 react js 中的 arrayList?

我想从文本框中获取值并将其推送到 Modules 数组,以便我可以通过迭代来呈现值。

我尝试使用 ref 但出现错误。

你能帮帮我吗?

 constructor(props) {
            super(props);
          this.state={
            module:'',
            Modules: []
          }
        }

        change (event){
            this.setState({
              [event.target.name]:event.target.value
            });
          };

        createModule (e) {
          e.preventDefault();
          console.log("submitted",this.state.module);
          this.setState(previousState => ({
              ...state,
              thisModules: [...previousState.Modules, 'new value']
          }));
        };

      render(){
        return(

            <form className="form-inline">
                  <div className="form-group">
                            Module Name:
                            <input type="text" id="module" 
                                name="module" 
                                  placeholder="module"  
                                    className="form-control" 
                                      ref="Module"
                                        value ={this.state.module}
                                          onChange={event => this.change(event)}/>
                  <button type="submit" className="btn btn-primary" onClick={(event) => this.createModule(event)}>Add Module</button>
                    </div>
            </form>

【问题讨论】:

  • 到目前为止你的代码是什么样的?可以发一下吗?
  • 我想获取文本字段值并将值推送到 ArrayList,以便我可以呈现这些值。

标签: reactjs react-redux


【解决方案1】:

mylist.push(document.getElementById('textbox_id').value)

将 mylist 作为您的列表,将 textbox_id 作为文本框的 id 应该可以工作。

认为这是普通的 javascript,因为我看不出与 react 有什么区别。

【讨论】:

    【解决方案2】:

    运行代码的主要问题是,在createModule 中,您没有将this 放在state 前面。如果您提供了错误的详细信息,这会有所帮助。

    还有一些其他错别字,下面是一个工作解决方案。

    class ModuleList extends React.Component {
      constructor(props) {
        super(props);
        this.state={
          module:'',
          Modules: []
        }
      }
    
      change (event){
        this.setState({
          [event.target.name]:event.target.value
        });
      }
    
      createModule (e) {
        e.preventDefault();
        console.log("submitted",this.state.module);
        this.setState(previousState => ({
          ...this.state,
          Modules: [...previousState.Modules, this.state.module]
        }));
      };
    
      render(){
        return (
          <form className="form-inline">
            <div className="form-group">
              Module Name:
                <input type="text" id="module" 
                  name="module" 
                  placeholder="module"  
                  className="form-control" 
                  ref="Module"
                  value={this.state.module}
                  onChange={event => this.change(event)}/>
                <button type="submit" className="btn btn-primary" onClick={(event) => this.createModule(event)}>Add Module</button>
              </div>
            
              <ul>
              {
                this.state.Modules.map(
                  (m) => <li>{m}</li>
                )
              }
              </ul>
            </form>
          );
      }
    }
    
    ReactDOM.render(
      <ModuleList />,
      document.getElementById('root')
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <p>My App:</p>
    <div id='root' />

    【讨论】:

    • 谢谢皮兰,非常感谢。我犯了一些错误。感谢指导。还有一件事,我如何将模块数组从该组件传递给其他组件,以及如何访问该组件中的数组。
    • @mohan 这确实是一个非常不同的问题,应该在单独的 SO 问题中提出。这取决于您是否要将模块传递给内部组件、父组件或任何其他组件。如果答案是后者,那么你应该开始考虑使用 redux 或类似的库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2011-08-02
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多