【发布时间】: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