2017-12-10 19 views
-2

내부에 형태가있는 반응 컨테이너가 있습니다. 양식에 세 개의 라디오 버튼이 있습니다. 각 라디오 버튼 입력의 각 값을 내 감속기에있는 객체 배열에서 가져온 객체로하고 싶습니다. 나는 라디오 버튼의 입력 값을 CONSOLE.LOG 그러나,이 얻을 :왜 내가 객체 대신 객체 객체를 얻고 있습니까

[개체 개체] 나 [개체 개체] 객체의 디폴트의 toString 표현 인 것을 알고

자바 스크립트에서,하지만 어떻게 그것의 내부 정보를 사용할 수있는 실제 개체를 잡아 수 있습니까? 여기

class NoteInput extends React.Component { 
    constructor(props) { 
     super(props); 

     this.state={ 
      selectedValue: null, 
     } 

    this.handleChange = this.handleChange.bind(this); 
    this.handleSubmit = this.handleSubmit.bind(this); 
    } 

    handleChange(e) { 
     this.setState({selectedValue: e.target.value}) 
    } 

    handleSubmit(e) { 
     e.preventDefault(); 
     console.log(this.state.selectedValue); 
    } 

    render() { 
     var inputs = this.props.locations.map((location, i) => { 
      return (
       <div key={i}> 
        <input type="radio" id={i} name="location" value={location} /> 
        <label htmlFor={'choice' + {i}}>{location.name}</label> 
       </div> 
      ); 
     }); 
     return (
      <div> 
       <form onSubmit={this.handleSubmit} onChange={this.handleChange} > 
        {inputs} 
        <input type="submit" value="Submit" /> 
       </form> 
      </div> 
     ); 
    } 
} 

내 감속기는 다음과 같습니다 :

export default function() { 
    return [ 
     {name: 'Safa Park', locationLat: '25.184992', locationLong: '55.248140'}, 
     {name: 'Mercato', locationLat: '25.217054', locationLong: '55.253051'}, 
     {name: 'Burj Khalifa', locationLat: '25.197787', locationLong: '55.274862'} 
    ] 
} 
+0

객체는 입력 필드의 값으로 사용될 수 없다. 객체를 식별하는 문자열 (이름, 배열의 인덱스 등)을 선택해야합니다. – JJJ

+0

[JavaScript 객체의 내용 인쇄?] (https://stackoverflow.com/questions/1625208/print- content-of-javascript-object) –

답변

0

당신은 radio button의 값으로 stringify 형식으로 location 객체를 저장할 수

여기 내 코드입니다.

var inputs = this.props.locations.map((location, i) => { 
     let strLoc = JSON.stringify(location); // stringify it 
     return (
     <div key={i}> 
      <input type="radio" id={i} name="location" value={strLoc} /> 
      <label htmlFor={'choice' + { i }}>{location.name}</label> 
     </div> 
    ); 
    }); 

handleSubmit은 json/object 형식으로 되돌아 갈 수 있습니다.

handleSubmit(e) { 
    e.preventDefault(); 
    let strLoc = JSON.parse(this.state.selectedValue); //parse it back to json/object 
    console.log(strLoc.name); 
    } 

작업 codesandbox demo

+0

고마워요. 이제 로그를 콘솔에 올리면 작동합니다. :) – user74843