【问题标题】:Radio buttons not showing up in ReactReact 中没有显示单选按钮
【发布时间】: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


【解决方案1】:

标记看起来没有任何问题。我怀疑样式阻止了单选按钮的显示。在图像上,所有内容都居中 - h5 标题是否有可能在单选按钮上呈现。如果您检查网页的源代码并在 DOM 中看到表明样式是问题的单选按钮。

【讨论】:

  • 感谢您的回复。你可能是对的,因为它确实出现在 h5 下的控制台中...
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
相关资源
最近更新 更多