• 主页
  • 单选按钮不显示在React中

单选按钮不显示在React中

我试图在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

转载请注明出处:http://www.jxbyjx.net/article/20230503/2482902.html