Forms 为什么我得到的是[对象]而不是对象
我有一个容器,里面有一个表单。表单包含三个单选按钮。我希望每个单选按钮输入的每个值都是从减速器中的对象数组中获取的对象。但是,当I console.log记录单选按钮输入的值时,我得到以下结果: [对象] 我知道[object object]是javascript中对象的默认toString表示形式,但是如何获取实际对象以便使用其中的信息呢 这是我的密码:Forms 为什么我得到的是[对象]而不是对象,forms,reactjs,redux,react-redux,javascript-objects,Forms,Reactjs,Redux,React Redux,Javascript Objects,我有一个容器,里面有一个表单。表单包含三个单选按钮。我希望每个单选按钮输入的每个值都是从减速器中的对象数组中获取的对象。但是,当I console.log记录单选按钮输入的值时,我得到以下结果: [对象] 我知道[object object]是javascript中对象的默认toString表示形式,但是如何获取实际对象以便使用其中的信息呢 这是我的密码: class NoteInput extends React.Component { constructor(props) {
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>
);
}
}
您可以将
位置
对象存储为字符串化
格式,作为单选按钮
的值
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>
);
});
工作对象不能用作输入字段值。您必须选择一个字符串来标识对象(名称,数组中的索引,…)可能是Thank You的重复项,它工作正常,在我现在登录控制台时出现:)
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(e) {
e.preventDefault();
let strLoc = JSON.parse(this.state.selectedValue); //parse it back to json/object
console.log(strLoc.name);
}