【发布时间】:2021-05-15 19:55:39
【问题描述】:
我正在尝试在 react 中创建一个非常简单的单选按钮系统,但无法正确呈现单选按钮。我附上了我的代码以及输出的样子。我该怎么做才能让单选按钮正确显示?谢谢!
https://i.imgur.com/N0ik047.png
constructor (props) {
super(props);
var user = props.location.state;
this.state = {
selected: ''
};
}
onChange = e => {
this.setState({selected: e.target.value})
}
render() {
return(
<form onSubmit={this.handleSubmit}>
<h1> Welcome! </h1>
<h4> Are you a: </h4>
<ul>
<li>
<label>
<input
type="radio"
value="student"
checked={value === "student"}
onChange={this.onChange}
/>
<h5> Student </h5>
</label>
</li>
<li>
<label>
<input
type="radio"
value="professor"
checked={value === "professor"}
onChange={this.onChange}
/>
<h5> Professor </h5>
</label>
</li>
</ul>
<button type="submit">Submit</button>
</form>
)
}
}
[1]: https://i.stack.imgur.com/DmU0W.png
【问题讨论】:
-
请在问题中提供图片,而不是外部链接。
-
checked={value === "professor"}在我看来很奇怪。我在任何地方都没有看到value的定义。
标签: javascript reactjs radio-button components render