【问题标题】:React with bootstrap and Radio buttons使用引导程序和单选按钮做出反应
【发布时间】:2020-06-24 09:07:35
【问题描述】:

我正在尝试在 React 中使用单选按钮(我一直使用它),但这次是 react-bootstrap。 我有两个单选按钮,我只想检查一个并获得该值。

import React, { useState } from "react";
import { Form, Button } from "react-bootstrap";

const StandAdd = () => {
  const [item, setItem] = useState({
    kindOfStand: ""
  });

  const { kindOfStand } = item;

  const handleInputChange = event => {
    event.preventDefault();
    setItem({ ...item, [event.target.name]: event.target.value });
  };

  const handleSubmit = event => {
    event.preventDefault();
    alert(`${kindOfStand}`);
  };

  return (
    <form onSubmit={handleSubmit}>
      <Form.Group controlId="kindOfStand">
        <Form.Check
          type="radio"
          aria-label="radio 1"
          label="Design"
          onChange={handleInputChange}
          checked={kindOfStand === "design"}
          // value="design" // <-- once enabled I can't select it
        />
        <Form.Check
          type="radio"
          aria-label="radio 2"
          label="Food"
          onChange={handleInputChange}
          checked={kindOfStand === "food"}
          // value="food" // <-- once enabled I can't select it
        />
      </Form.Group>
      <Button variant="primary" type="submit">
        Submit
      </Button>
    </form>
  );
};

export default StandAdd;

代码沙盒链接:https://codesandbox.io/s/react-bootstrap-radio-button-1d443

关于如何获得价值的任何想法?

【问题讨论】:

    标签: javascript reactjs react-bootstrap


    【解决方案1】:

    Form.Checks 赋予value 属性,然后像这样单击单选按钮后使用e.target.value 获取值。使用e.persist(); 避免出现以下错误:

    出于性能原因,会重复使用此合成事件。如果你是 看到这一点,您正在访问方法 currentTarget 上 释放/无效合成事件

    const StandAdd = () => {
      const [item, setItem] = useState({ kindOfStand: "", another: "another" });
    
      const { kindOfStand } = item;
    
      const handleChange = e => {
        e.persist();
        console.log(e.target.value);
    
        setItem(prevState => ({
          ...prevState,
          kindOfStand: e.target.value
        }));
      };
    
      const handleSubmit = e => {
        e.preventDefault();
        alert(`${kindOfStand}`);
      };
    
      return (
        <form onSubmit={handleSubmit}>
          <Form.Group controlId="kindOfStand">
            <Form.Check
              value="design"
              type="radio"
              aria-label="radio 1"
              label="Design"
              onChange={handleChange}
              checked={kindOfStand === "design"}
            />
            <Form.Check
              value="food"
              type="radio"
              aria-label="radio 2"
              label="Food"
              onChange={handleChange}
              checked={kindOfStand === "food"}
            />
          </Form.Group>
          <Button variant="primary" type="submit">
            Submit
          </Button>
        </form>
      );
    };
    

    Demo

    【讨论】:

    • 谢谢,但它没有任何价值,也不返回任何价值
    • 对不起,请重新检查
    • 是的,谢谢。但是我必须用其他值创建一个item,并且我必须保持语法const [item, setItem] = useState({ kindOfStand: "" }); 等等。这不起作用
    • 好的。用你原来的状态再次改变它。看看吧。
    • 谢谢。有用。我还发现了另一种使用切换按钮的方法
    猜你喜欢
    • 2021-11-13
    • 2015-09-11
    • 2020-04-08
    • 2020-05-27
    • 2017-08-27
    • 1970-01-01
    • 2019-11-16
    • 2015-10-27
    • 2021-12-06
    相关资源
    最近更新 更多