响应更改输入值onChange

时间:2018-03-19 11:50:16

标签: reactjs

这是我的 SearchForm.js handleKeywordsChange必须处理输入keywords更改

import React from 'react';
import ReactDOM from 'react-dom';

class SearchForm extends React.Component {
    constructor(props) {
      super(props)

      this.state = {
       keywords: '',
       city: '',
       date: ''     
      }

      //this.handleChange = this.handleChange.bind(this)
      //this.handleSubmit = this.handleSubmit.bind(this)

      this.handleKeywordsChange = this.handleKeywordsChange.bind(this);
     }

    handleKeywordsChange(e) {
        console.log(1);

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

    render() {
        return ( 
            <form className='form search-form' onSubmit={this.handleSubmit}>
                <div className="form-row">
                  <div className="form-group col-md-5">
                    <label htmlFor="keywords">Keywords</label>
                    <input type="text" className="form-control" name="keywords" id="keywords" placeholder="Keywords" onChange={this.handleKeywordsChange} value={this.state.keywords} />

                  </div>

                  <div className="form-group col-md-5">
                    <label htmlFor="city">City</label>
                    <input type="text" className="form-control" name="city" id="city" placeholder="City" onChange={this.handleChange} value={this.state.city} />
                  </div>

                  <div className="form-group col-md-2">
                    <label htmlFor="date">Date</label>
                    <select className="form-control" name="date" id="date" onChange={this.handleChange} value={this.state.date}>
                      <option>1</option>
                      <option>2</option>
                      <option>3</option>
                      <option>4</option>
                      <option>5</option>
                    </select>
                  </div>
                 </div>

                 <div className="form-row">
                     <div className="form-group col-md-12">
                        <input id='formButton' className='btn btn-primary' type='submit' placeholder='Send' />
                    </div>
                 </div>
            </form>
        )
    }
}

export { SearchForm }

问题是输入keywords在我输入时不会改变其值。怎么了?

6 个答案:

答案 0 :(得分:4)

创建一个用于更改输入值状态的常用功能。

handleInputChange(e) {
    this.setState({
        [e.target.name]: e.target.value
    });
}

请务必在每个name代码中提及input。例如:

<input name="someUniqueName" value={this.state.someState} onChange={this.handeInputChange} />

答案 1 :(得分:2)

应该是:

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

答案 2 :(得分:1)

您的handleKeywordsChange函数设置状态value,而您使用this.state.keywords作为输入值

handleKeywordsChange(e) {
    console.log(1);

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

答案 3 :(得分:1)

class InputKeywordCheck {
state = {
    email: '',
}

handleInputChange (e) {
    const {name, value } = e.target;
    this.setState({[name]: value});
}

render() {
    return (
        <input name="email" value={this.state.email} onChange={this.handleInputChange} />
    )
} }

答案 4 :(得分:0)

React Hooks使得操作变得如此简单!!!

Aggregate

答案 5 :(得分:0)

我相信你需要做这样的事情:

handleKeyWordsChange (e)  {
    this.setState({[e.target.name]: e.target.value});
}