用React提交后清除输入字段

时间:2018-12-07 04:08:00

标签: reactjs

我正在用React编写评论表格。除了这个小问题,我几乎已经构建了所需的东西,在提交表单后,我无法清除输入值。即使输入后,我输入的内容仍保留在输入字段中。

我的代码:

CommentsIndex.js

class CommentsIndex extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      url: this.props.url,
      comment: '',
    };
    this.onSubmit2 = this.onSubmit2.bind(this);
  }
  loadCommentsFromServer() {
    $.ajax({
      // ajax call working fine
    });
  }
  onSubmit2(value) {
    const url = this.props.url;
    $.ajax({
      url: url,
      dataType: 'text',
      type: 'POST',
      cache: false,
      data: {
        comment: value
      },
      success: (data) => {
        this.loadCommentsFromServer();
      },
      error: (xhr, status, err) => {
        console.error(url, status, err.toString());
      },
    });
  }
  componentDidMount() {
    this.loadCommentsFromServer();
  }
  render() {
    return (
      <div>
        <NewComment onSubmit1={this.onSubmit2} />
        <Comments comments={this.state.comments} />
      </div>
    );
  }
}

export default CommentsIndex;

NewComment.js

class NewComment extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      comment: ''
    };
    this.getInput = this.getInput.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }
  getInput(e) {
    this.setState({
      comment: e.target.value
    });
  }
  onSubmit() {
    this.props.onSubmit1(this.state.comment);
  }
  render() {
    return (
      <div>
        <input className="comment-field" onChange={this.getInput} />
        <input className="comment-submit" type="submit" value="SUBMIT" onClick={this.onSubmit} />
      </div>
    );
  }
}

export default NewComment;

我尝试在this.setState({ value: '' })的{​​{1}}内添加success: (data) => {},但是没有用。

任何建议将不胜感激。预先感谢!

3 个答案:

答案 0 :(得分:1)

成功执行ajax请求后,将状态设置为”,如下所示:

this.setState({
  comment: ''
});

并将状态绑定到输入字段,如下所示:

<input value={this.state.comment} className="comment-field" onChange={this.getInput} />

答案 1 :(得分:1)

在下面添加诸如ref={(input)=>this.commentInput=input}之类的引用:

<div>
    <input ref={(input)=>this.commentInput=input} className="comment-field" onChange={this.getInput} />
    <input className="comment-submit" type="submit" value="SUBMIT" onClick={this.onSubmit} />
</div>

并在您的onSubmitMethod中执行如下代码:

onSubmit() {
    this.props.onSubmit1(this.state.comment);
    this.commentInput.value='';
}

在对道具进行回调后将值“''设置为

答案 2 :(得分:0)

如果输入元素位于form内,则

form.reset()

可用于在提交时重置所有表单组件。

class App extends React.Component {
  constructor(props) {
    super(props);
    this.onSubmit = this.onSubmit.bind(this);
  }
  onSubmit(event) {
    event.preventDefault();
    const form = event.target;
    const name = event.target.name.value;
    console.log(name);
    form.reset();
  }
  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <input type="text" name="name" placeholder="Name" />
        <button>Submit</button>
      </form>
    );
  }
}
ReactDOM.render(<App />, document.getElementById("root"));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>