2017-02-23 89 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的说法。