2017-02-23 85 views
0

當我嘗試將輸入組件中的值發送到console.log函數中的句柄函數時,我接收到變量cName爲undefined的event.target.name。event.target.name給予未定義的

@connect((store) => { 
      return { 
       nameOfCity:store.nameOfCity.nameOfCity, 
       weatherDescription:store.weatherDescription.weatherDescription, 
       windSpeed:store.windSpeed.windSpeed, 
       temperature:store.temperature.temperature, 
       maxTemperature:store.maxTemperature.maxTemperature, 
       minTemperature:store.minTemperature.minTemperature, 
      } 
     }) 

     class FormContainer extends Component { 

      handleFormSubmit(e) { 
       e.preventDefault(); 
       let cName = event.target.name; 
       console.log(cName); 
       this.props.dispatch(fetchWeatherData(cName)); 
      } 

      render() { 
       return (
        <div> 
        <form onSubmit={this.handleFormSubmit.bind(this)}> 
         <label>{this.props.label}</label> 
         <SearchBar 
          name="CityName" 
          type="text" 
          value={this.props.cityName} 
          placeholder="search" 
         /> 

         <button type="submit" className="" value='Submit' placeholder="Search">Search</button> 
        </form> 
        </div> 
       ); 
      } 
     } 

搜索欄組件

const SearchBar = (props) => (
     <div> 
      <label>{props.label}</label> 
      <input name={props.name} type="text" defaultValue={props.value} placeholder={props.placeholder}/> 
     </div> 
    ); 
    export default SearchBar; 

編輯:這是爲什麼VAR不確定應該不是傳遞

+0

電腦總是正確的。這個變量可能確實是未定義的。 – Roland

+0

我加了一個問題 – OunknownO

+1

因爲'handleFormSubmit'中的參數被稱爲'e'而不是'event'。應該是'e.target.name' – VanDanic

回答

1

你傳入事件e變量handleFormSubmit,因此event對象是不確定的那裏,並且因爲它沒有定義在哪裏,它會正確返回undefined

只要將e改爲event就可以了handeFormSubmit的說法。