如何设置单选按钮默认签入反应?

Posted

技术标签:

【中文标题】如何设置单选按钮默认签入反应?【英文标题】:How to Set radio button default checked in react? 【发布时间】:2019-11-23 13:54:30 【问题描述】:

我创建了带有 3 个单选按钮的单选按钮: 我想将石灰作为默认选中单选按钮,我将石灰设置为默认值但它不起作用。

这是我的代码,我不知道如何解决我的问题。

    import React, Component from 'react';

    class App extends Component
      constructor(props)
        super(props)

        this.handleflavorSubmit = this.handleflavorSubmit.bind(this)
        this.onChangeRadio = this.onChangeRadio.bind(this)


        this.state = value : 'lime';
        

      onChangeRadio(e)
        this.setState(value : e.target.value)
      

      handleflavorSubmit(e)
        alert("your favorite flavor is " + this.state.value)
        e.preventDefault();
      

      render()

        return( 
          <div>
            <h1>Choose your flavor:</h1>
            <form onSubmit = this.handleflavorSubmit>
              <input type="radio" checked = this.state.value === 'grapefruit' onChange = this.onChangeRadio value= "grapefruit"/>Grapefruit
              <input type = "radio" checked = this.state.value === 'lime' onChange = this.onChangeRadio value = "lime"/>Lime
              <input type = "radio" checked = this.state.value === 'orange' onChange = this.onChangeRadio value = "orange"/>Orange
              <input type = "submit" value = "submit"/>
            </form>

          </div>
        )
      
    

export default App

【问题讨论】:

【参考方案1】:

您的代码实际上正在运行。您只需要在返回后包含括号即可。

在 CodeSandbox 中尝试一下。这是工作代码:https://codesandbox.io/s/red-rain-r81n4

import React,Component from "react";
import ReactDOM from "react-dom";


import "./styles.css";

class App extends Component
  constructor()
    super()

    this.handleflavorSubmit = this.handleflavorSubmit.bind(this)
    this.onChangeRadio = this.onChangeRadio.bind(this)


    this.state = value : 'lime';
    

  onChangeRadio(e)
    this.setState(value : e.target.value)
  

  handleflavorSubmit(e)
    alert("your favorite flavor is " + this.state.value)
    e.preventDefault();
  

  render()

    return (
      <div>
        <h1>Choose your flavor:</h1>
        <form onSubmit = this.handleflavorSubmit>
          <input type="radio" checked = this.state.value === 'grapefruit' onChange = this.onChangeRadio value= "grapefruit"/>Grapefruit
          <input type = "radio" checked = this.state.value === 'lime' onChange = this.onChangeRadio value = "lime"/>Lime
          <input type = "radio" checked = this.state.value === 'orange' onChange = this.onChangeRadio value = "orange"/>Orange
          <input type = "submit" value = "submit"/>
        </form>
      </div>
    );


const rootElement = document.getElementById("root");
ReactDOM.render(<App/>, rootElement);

【讨论】:

【参考方案2】:

为您要设置为选中的input 无线电添加defaultChecked 属性,使其在第一次装载时处于选中状态。

<input type = "radio" defaultChecked checked = this.state.value === 'lime' onChange = this.onChangeRadio value = "lime"/>Lime

【讨论】:

我添加了 defaultChecked 但仍然不起作用。无论如何谢谢你,我不知道。 我已经测试了你分享的示例代码,它工作正常,checkout 我遇到了类似的问题,经过数小时的反复试验,我只需将我的代码从 defaultChecked=value === 'string' 更改为 defaultChecked=value === 'string' ? "true" : "false"

以上是关于如何设置单选按钮默认签入反应?的主要内容,如果未能解决你的问题,请参考以下文章

html中<radio>单选按钮控件标签用法解析及如何设置默认选中

在一组单选按钮中怎样设置默认选项呢/在哪里设置呢?

html中<radio>单选按钮控件标签用法解析及如何设置默认选中

如何在反应引导程序中使用单选按钮

在反应中管理循环单选按钮的状态

如何按状态值更改选中(选定)单选按钮[重复]