【发布时间】:2019-04-13 01:39:43
【问题描述】:
我无法根据所选单选按钮呈现信息。应该发生的是当单击是按钮时,应该出现“欢迎用户”。选择否时,应出现“请登录”。
我在州方面也遇到了麻烦。当我最初单击一个按钮时,控制台按钮将我的状态显示为空白。然后当我单击否时,状态更新为是,反之亦然。不知道这里发生了什么。
render() {
return (
<form onSubmit={this.onFormSubmit}>
<p>Did you go to the lecture?</p>
<label>
<input
type="radio"
value="No"
checked={this.state.goToLectue === 'No'}
onChange={this.handleChange}
/>
No
</label>
<label>
<input
type="radio"
value= "Yes"
checked={this.state.goToLectue === 'Yes'}
onChange={this.handleChange}
/>
Yes
</label>
<div>
<Response goToLecture = {this.state.goToLectue}/>
</div>
);
}
onFormSubmit = e => {
e.preventDefault();
console.log('Chosen: ' + this.state.goToLectue);
};
handleChange = e => {
this.setState({
goToLectue: e.target.value
});
console.log('value of goToLectue onchange: ' + this.state.goToLectue);
}
组件外的功能
function Response(props) {
if (props.goToLectue)
return <h1>Welcome User</h1>;
else
return <h1>Please Login</h1>;
}
【问题讨论】:
标签: reactjs radio-button