React.js - 如何设置选中/选中的单选按钮并跟踪onChange?

时间:2017-06-15 21:57:19

标签: javascript forms reactjs radio-button

我在一个组件中有一个星期几的广播组。如果用户已经有一天与他们的帐户相关联,我希望它是选中/选中的单选按钮。因此,如果他们之前保存了“星期一”,我将从父级获取并将其存储在状态中,并希望在页面呈现时选择它。

我试图将它设置为类似于React Forms for select中的方式,但它似乎不适用于Fieldset。有什么想法吗?

constructor(props) {
  super(props);
  this.state = {
    reportWeekday: "monday"
  }
}

  handleWeekdayChange(event){
    this.setState({reportWeekday: event.target.value});
    console.log(event.target.value);
  }

  <fieldset className="schedule-weekday" value={this.state.reportWeekday} onChange={this.handleWeekdayChange}>
    <label for="sunday"><input type="radio" name="schedule-weekly-option" value="sunday" id="sunday" />Sunday</label>
    <label for="monday"><input type="radio" name="schedule-weekly-option" value="monday" id="monday" />Monday</label>
    <label for="tuesday"><input type="radio" name="schedule-weekly-option" value="tuesday" id="tuesday" />Tuesday</label>
    <label for="wednesday"><input type="radio" name="schedule-weekly-option" value="wednesday" id="wednesday" />Wednesday</label>
    <label for="thursday"><input type="radio" name="schedule-weekly-option" value="thursday" id="thursday" />Thursday</label>
    <label for="friday"><input type="radio" name="schedule-weekly-option" value="friday" id="friday" />Friday</label>
    <label for="saturday"><input type="radio" name="schedule-weekly-option" value="saturday" id="saturday" />Saturday</label>
  </fieldset>

3 个答案:

答案 0 :(得分:2)

这是Jonny的解决方案,没有启用类属性。

class ControlledRadios extends React.Component{
  constructor(){
    super()
    this.state = {
      reportWeekday: 'monday'
    }
  }

  handleWeekdayChange(event) {
    this.setState({reportWeekday: event.target.value})    
  }

  render() {
    let {reportWeekday} = this.state
    return (<fieldset onChange={this.handleWeekdayChange.bind(this)}>
      <label><input type="radio" name="schedule-weekly-option" value="sunday" checked={reportWeekday === 'sunday'}/>Sunday</label>
      <label><input type="radio" name="schedule-weekly-option" value="monday" checked={reportWeekday === 'monday'}/>Monday</label>
      <label><input type="radio" name="schedule-weekly-option" value="tuesday" checked={reportWeekday === 'tuesday'}/>Tuesday</label>
      <label><input type="radio" name="schedule-weekly-option" value="wednesday" checked={reportWeekday === 'wednesday'}/>Wednesday</label>
      <label><input type="radio" name="schedule-weekly-option" value="thursday" checked={reportWeekday === 'thursday'}/>Thursday</label>
      <label><input type="radio" name="schedule-weekly-option" value="friday" checked={reportWeekday === 'friday'}/>Friday</label>
      <label><input type="radio" name="schedule-weekly-option" value="saturday" checked={reportWeekday === 'saturday'}/>Saturday</label>
    </fieldset>)
    }

}

答案 1 :(得分:1)

似乎在多个同名的uncontrolled单选按钮上设置defaultChecked并不像您期望的那样工作,并且由于某种原因,onChange每个不受控制的单选按钮只会触发一次(使用react@15.6.1),因此您可能需要通过设置checked切换到controlled输入。

class ControlledRadios extends React.Component {
  state = {
    reportWeekday: 'monday'
  }

  handleWeekdayChange = (event) => {
    this.setState({reportWeekday: event.target.value})
  }

  render() {
    let {reportWeekday} = this.state
    return <fieldset onChange={this.handleWeekdayChange}>
      <label><input type="radio" name="schedule-weekly-option" value="sunday" checked={reportWeekday === 'sunday'}/>Sunday</label>
      <label><input type="radio" name="schedule-weekly-option" value="monday" checked={reportWeekday === 'monday'}/>Monday</label>
      <label><input type="radio" name="schedule-weekly-option" value="tuesday" checked={reportWeekday === 'tuesday'}/>Tuesday</label>
      <label><input type="radio" name="schedule-weekly-option" value="wednesday" checked={reportWeekday === 'wednesday'}/>Wednesday</label>
      <label><input type="radio" name="schedule-weekly-option" value="thursday" checked={reportWeekday === 'thursday'}/>Thursday</label>
      <label><input type="radio" name="schedule-weekly-option" value="friday" checked={reportWeekday === 'friday'}/>Friday</label>
      <label><input type="radio" name="schedule-weekly-option" value="saturday" checked={reportWeekday === 'saturday'}/>Saturday</label>
    </fieldset>
  }
}

答案 2 :(得分:0)

对于使用UseState挂钩的用户,您可以简单地做到这一点:

    import React, { useState } from 'react';
    
    
    export default function ControlledRadios() {
    const [weekday,setWeekday] = useState("monday")
    
    function handleWeekdayChange(event) {
    setWeekday(event.target.value)
    }
    
    return {
    <fieldset className="schedule-weekday" value={weekday} onChange={(event) => handleWeekdayChange(event)}>
        <label for="sunday"><input type="radio" name="schedule-weekly-option" value="sunday" id="sunday" />Sunday</label>
        <label for="monday"><input type="radio" name="schedule-weekly-option" value="monday" id="monday" />Monday</label>
        <label for="tuesday"><input type="radio" name="schedule-weekly-option" value="tuesday" id="tuesday" />Tuesday</label>
        <label for="wednesday"><input type="radio" name="schedule-weekly-option" value="wednesday" id="wednesday" />Wednesday</label>
        <label for="thursday"><input type="radio" name="schedule-weekly-option" value="thursday" id="thursday" />Thursday</label>
        <label for="friday"><input type="radio" name="schedule-weekly-option" value="friday" id="friday" />Friday</label>
        <label for="saturday"><input type="radio" name="schedule-weekly-option" value="saturday" id="saturday" />Saturday</label>
      </fieldset>
    }