3 回答
TA贡献1772条经验 获得超8个赞
您需要使用已检查
const { useState } = React;
const App = () => {
const [formValue, setFormValue] = useState({
UserName: "Test User",
Relocation: true,
ContactMode: "Email",
});
const handleInputChange = (e) => {
const { name, value } = event.target;
setFormValue({
...formValue,
[name]: value,
});
};
const handleCheckedChange = (e) => {
const { name, checked } = event.target;
setFormValue({
...formValue,
[name]: checked,
});
}
const submitData = () => {
console.log(formValue)
}
return (
<div>
<form>
<label> User Name </label>
<input
type="text"
name="UserName"
value={formValue.UserName}
onChange={handleInputChange}
/>
<br />
<br />
<label> Willing to relocate? </label>
<input
type="checkbox"
name="Relocation"
checked={formValue.Relocation}
onChange={handleCheckedChange}
/>
<br />
<br />
<label> Contact Mode </label>
<input
type="radio"
name="ContactMode"
checked={formValue.ContactMode === "Email"}
value={`Email`}
onChange={handleInputChange}
/>{" "}
<input
type="radio"
name="ContactMode"
checked={formValue.ContactMode === "Text"}
value={`Text`}
onChange={handleInputChange}
/>{" "}
Text
<input
type="radio"
name="ContactMode"
checked={formValue.ContactMode === "Both"}
value={`Both`}
onChange={handleInputChange}
/>{" "}
Both
<br />
<br />
<button type="button" onClick={submitData}> Submit </button>
</form>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>
TA贡献1946条经验 获得超4个赞
value复选框的等效属性是checked:
<input
type="checkbox"
name="Relocation"
checked={formValue.Relocation}
onChange={handleInputChange}
/>
TA贡献1853条经验 获得超18个赞
const { useState } = React;
const App = () => {
const [formValue, setFormValue] = useState({
UserName: "Test User",
Relocation: true,
ContactMode: "Email",
});
const handleInputChange = (e) => {
const { name, value } = event.target;
setFormValue({
...formValue,
[name]: value,
});
};
const submitData = () => {
console.log(formValue)
}
return (
<div>
<form>
<label> User Name </label>
<input
type="text"
name="UserName"
value={formValue.UserName}
onChange={handleInputChange}
/>
<br />
<br />
<label> Willing to relocate? </label>
<input
type="checkbox"
name="Relocation"
value={formValue.Relocation}
onChange={handleInputChange}
/>
<br />
<br />
<label> Contact Mode </label>
<input
type="radio"
name="ContactMode"
value={`Email`}
onChange={handleInputChange}
checked={formValue.ContactMode==="Email"}
/>{" "}
<input
type="radio"
name="ContactMode"
value={`Text`}
onChange={handleInputChange}
checked={formValue.ContactMode==="Text"}
/>{" "}
Text
<input
type="radio"
name="ContactMode"
value={`Both`}
onChange={handleInputChange}
checked={formValue.ContactMode==="Both"}
/>{" "}
Both
<br />
<br />
<button type="button" onClick={submitData}> Submit </button>
</form>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
添加回答
举报