如何使用单选按钮更改此状态

时间:2019-09-26 13:34:14

标签: reactjs radio-button state

我想通过选择要用作参数以将信息发送到api的这些状态单选按钮中的任意一个来更改一些可选按钮

import React ,{Component} from 'react';
import axios from "axios/index";

export default class Test extends Component {

    constructor(props) {
        super(props);
        this.state = {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    }

这是我的axios代码

componentDidMount(){
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

这是我的按钮

render() {
    return (
        <div className="container-fluid">
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Justice" />
                        options1-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Credit" />
                        options1-2
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="New" />
                        options2-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="LowPerformance" />
                        options2-2
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="Old" />
                        options2-3
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Unregistered" autoComplete="off" />
                        options3-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Registered" />
                        options3-2
                    </label>
                </div>
            </div>
        </div>
    );
}

请帮助。我想单击这些按钮中的任何一个,以将一个类添加到其标签中,而每个组都是一个选项。

2 个答案:

答案 0 :(得分:1)

您应该在value输入中提供onChangeradio道具。

<div className="row RegisterOptions">
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Justice" value="Justice" onChange={this.changeHandle}/>
            options1-1
        </label>
    </div>
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Credit" value="Credit" onChange={this.changeHandle}/>
            options1-2
        </label>
    </div>
</div>

此处,您的值将根据单选输入而变化,但是changeHandle对于所有单选输入将保持不变。

您的changeHandle应该是这个

changeHandle = (e) => {
   this.setState({
     [e.target.name]: e.target.value
   })
}

Demo


然后可以将状态值用于axios调用。您应该为API调用创建一个单独的函数

callAPI = () => {
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: this.state.TypeStatus,
            OperatingStatus: this.state.OperatingStatus,
            RegistrationStatus: this.state.RegistrationStatus,
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

现在可以从任何地方调用此功能,例如单击“提交”按钮

<button onClick={this.callAPI}>Submit</button>

或来自componentDidMount

componentDidMount(){
   this.callAPI();
}

答案 1 :(得分:0)

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      TypeStatus: '',
      OperatingStatus: '',
      RegistrationStatus: '',
    };
  }

  // handleStateClick = () =>

  render() {
    return (
      <div>
        <button onClick={() => this.setState({ TypeStatus: " TypeStatus clicked" }, console.log(this.state.TypeStatus))}>TypeStatus</button>
        <button onClick={() => this.setState({ OperatingStatus: " OperatingStatus clicked" }, console.log(this.state.OperatingStatus))}>OperatingStatus</button>
        <button onClick={() => this.setState({ RegistrationStatus: " RegistrationStatus clicked" }, console.log(this.state.RegistrationStatus))}>RegistrationStatus</button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

https://stackblitz.com/edit/react-ahytle?file=index.js