多个文本字段onChange函数问题

时间:2018-03-05 12:57:10

标签: javascript reactjs onclick onchange

有一个加号按钮可以增加页面中的文本字段数。增加文本字段后,我想在单击提交按钮后立即显示所有文本字段输入值。 但是它在onChange setState问题上给出了一个问题。怎么处理?有什么帮助吗?

这里是=>的 DEMO

class App extends Component {

constructor(props) {
    super(props);
    this.state = { 
      arr: [],
      firstname: '',
      lastname: '',
      setarr: [],
      temp: []
    };
    this.addTextfields = this.addTextfields.bind(this);
    this.changeFirstname = this.changeFirstname.bind(this);
    this.changeLastname = this.changeLastname.bind(this);
  }

  addTextfields(e) {
    let htmlContent = [];
    htmlContent.push(
      <div>
        <input type="text" placeholder="first name" onChange={this.changeFirstname} value={this.state.firstname} /> <br/> <br/>
        <input type="text" placeholder="last name" onChange={this.changeLastname} value={this.state.lastname} /> <br/> <br/>
      </div>
    );
    this.setState({ arr: this.state.arr.concat(htmlContent) });
  }

  changeFirstname(e) {
    this.setState({ firstname: e.target.value });
  }

  changeLastname(e) {
    this.setState({ lastname: e.target.value });
  }

  showTexts() {
    console.log(this.state.firstname, this.state.lastname);
    var add = [this.state.firstname, this.state.lastname];
    this.state.temp = this.state.temp.concat(add);
    this.setState({ setarr: this.state.temp, firstname: '', lastname: '' });
    // localStorage.setItem(this.state.arr, 'names');
  }

  render() {
    return (
      <div>
        <div className="App">
          <input type="text" onChange={this.changeFirstname} placeholder="first name" value={this.state.firstname} /> <br/> <br/>
          <input type="text" onChange={this.changeLastname} placeholder="last name" value={this.state.lastname} /> <br/> <br/>
          <button type="submit" onClick={this.addTextfields}>Plus</button>
          {this.state.arr}
        </div>
        <div className="submit-button">
          <button type="submit" onClick={this.showTexts.bind(this)}>Submit</button>
        </div>
        <div>
          {this.state.setarr}
        </div>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

2 个答案:

答案 0 :(得分:1)

这样做。

import React, { Component } from "react";
import { render } from "react-dom";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      arr: [
        {
          firstname: "",
          lastname: ""
        }
      ],
      displayValues: []
    };
    this.addTextfields = this.addTextfields.bind(this);
    this.changeFirstname = this.changeFirstname.bind(this);
    this.changeLastname = this.changeLastname.bind(this);
  }

  addTextfields(e) {
    let arr = this.state.arr;
    arr.push({
      firstname: "",
      lastname: ""
    });
    this.setState({ arr });
  }

  changeFirstname(e, index) {
    let arr = this.state.arr;
    arr[index].firstname = e.target.value;
    this.setState({ arr });
  }

  changeLastname(e, index) {
    let arr = this.state.arr;
    arr[index].lastname = e.target.value;
    this.setState({ arr });
  }

  showTexts() {
    let displayValues = [];
    this.state.arr.map(element => {
      console.log(element.firstname, element.lastname);
      var add = element.firstname + " " + element.lastname;
      displayValues.push(add);
    });
    this.setState({ displayValues });
  }

  render() {
    return (
      <div>
        <div className="App">
          {this.state.arr.map((element, index) => {
            return (
              <div key={index}>
                <input
                  type="text"
                  onChange={e => this.changeFirstname(e, index)}
                  placeholder="first name"
                  value={this.state.firstname}
                />{" "}
                <br /> <br />
                <input
                  type="text"
                  onChange={e => this.changeLastname(e, index)}
                  placeholder="last name"
                  value={this.state.lastname}
                />{" "}
                <br /> <br />
              </div>
            );
          })}
          <button type="submit" onClick={this.addTextfields}>
            Plus
          </button>
        </div>
        <div className="submit-button">
          <button type="submit" onClick={this.showTexts.bind(this)}>
            Submit
          </button>
        </div>
        <div>
          {this.state.displayValues.map(element => {
            return <p>{element}</p>;
          })}
        </div>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

Edit zwln33q3vx

答案 1 :(得分:0)

  

我认为,你这样做是错误的。我创建了以下示例以供参考。请看看。

const Form = (props) => {
  return(
    <div>
        <input type="text" placeholder="first name" onChange={props.changeFirstname} value={props.firstname} /> <br/> <br/>
        <input type="text" placeholder="last name" onChange={props.changeLastname} value={props.lastname} /> <br/> <br/>
    </div>
  )
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { 
      arr: [
        {
          firstname: '',
          lastname: '',
          id:1
        }
      ],
      showOutput:false
    };
    this.addTextfields = this.addTextfields.bind(this);
    this.changeFirstName = this.changeFirstName.bind(this);
    this.changeLastName = this.changeLastName.bind(this);
  }

  addTextfields(e) {
    debugger
    let oldArray = this.state.arr;
    this.setState((prevState) => { 
      return {
        arr: oldArray.concat({
          firstname: '',
          lastname: '',
          id: prevState.arr.length + 2
        })
      }
    });
  }

  changeFirstName(e, selectedIndex) {
    let updatedArray =  this.state.arr.map((data) => {
      if(data.id === selectedIndex) {
        return Object.assign({}, data, {
          firstname: e.target.value
        })
      } else {
        return data
      }
    })

    this.setState({ arr: updatedArray });
    
  }

  changeLastName(e, selectedIndex) {
    let updatedArray =  this.state.arr.map((data) => {
      if(data.id === selectedIndex) {
        return Object.assign({}, data, {
          lastname: e.target.value
        })
      } else {
        return data
      }
    })
    this.setState({ arr: updatedArray });
  }

  showTexts() {
    this.setState({
      showOutput: true
    })
  }

  render() {
    return (
      <div>
        <div className="App">
          {
            this.state.arr.map((data, index) => {
              return <Form key={index} data={data} 
                      changeFirstname={(e) => this.changeFirstName(e, data.id)}
                      changeLastname={(e) => this.changeLastName(e, data.id)}
                    />
            })
          }
          <button type="submit" onClick={this.addTextfields}>Plus</button> 
        </div>
        <div className="submit-button">
          <button type="submit" onClick={this.showTexts.bind(this)}>Submit</button>
        </div>
        <div>
          {
            this.state.showOutput && 
            this.state.arr.map((data, index) => {
              return <div>
              {data.firstname}  {data.lastname}
              </div>
            })
          }
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, 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>
<div id="root"></div>

希望这有帮助。