Javascript 如何在react时控制defaultChecked-in输入标记?
如何在react控制初始默认签入输入标记 我是韩国人,想成为前端工程师。我不擅长使用英语。 我现在有个问题 我创建了调查页面,有一些选项可供选择。然后我想在调查页面上设置修改功能。因此,我可以渲染另一个输入标记值,但我不能渲染将为defaultChecked值的选项值 我这样设置Javascript 如何在react时控制defaultChecked-in输入标记?,javascript,reactjs,Javascript,Reactjs,如何在react控制初始默认签入输入标记 我是韩国人,想成为前端工程师。我不擅长使用英语。 我现在有个问题 我创建了调查页面,有一些选项可供选择。然后我想在调查页面上设置修改功能。因此,我可以渲染另一个输入标记值,但我不能渲染将为defaultChecked值的选项值 我这样设置defaultChecked={checkedValue} 和const[checkedValue,setCheckedValue]=useState(false) 然后我可以从数据库中接收值。这是从下面传来的, 值是从
defaultChecked={checkedValue}
和const[checkedValue,setCheckedValue]=useState(false)
然后我可以从数据库中接收值。这是从下面传来的,
值是从数据库监视数据之前的选项值
所以我想说
- 当我单击编辑按钮时,我想显示接收到的数据库数据中的选项值,然后我想控制defaultChecked值为true或false
const EditSurveyForm = ({ data, handleClick, name }) => {
const getToken = window.sessionStorage.getItem("token");
const [value, getValue] = useState("");
const [checkValue1, getCheckValue1] = useState(false);
const [checkValue2, getCheckValue2] = useState(false);
useEffect(() => {
const ac = new AbortController();
axios
.get(`http://localhost:5000/content/${contentId}`, {
headers: { "x-access-token": getToken },
})
.then(res => {
if (res.data.Content) {
getValue(res.data.Content[name]);
handleOption();
}
});
}, [value]);
const handleOption = () => {
if (value === 1) {
getCheckValue1(true);
}
if (value === 2) {
getCheckValue2(true);
}
};
return (
<>
<Container>
<div className={data.name}>{data.data}</div>
<SurveyContainer>
<span>
약1
<div>
<input
defaultChecked={checkValue1}
type="radio"
name={`question${data.id}`}
value="1"
onClick={e => handleClick(data.name, e.target.value)}
className="option1"
/>
</div>
</span>
<span>
<br></br>
<div>
<input
defaultChecked={checkValue2}
type="radio"
name={`question${data.id}`}
value="2"
onClick={e => handleClick(data.name, e.target.value)}
className="option2"
/>
</div>
</span>
</SurveyContainer>
</Container>
</>
);
};
export default EditSurveyForm;
const EditSurveyForm=({data,handleClick,name})=>{
const getToken=window.sessionStorage.getItem(“令牌”);
const[value,getValue]=useState(“”);
常量[checkValue1,getCheckValue1]=useState(false);
常量[checkValue2,getCheckValue2]=useState(false);
useffect(()=>{
const ac=新的中止控制器();
axios
.得到(`http://localhost:5000/content/${contentId}`{
标题:{“x-access-token”:getToken},
})
。然后(res=>{
if(res.data.Content){
getValue(res.data.Content[name]);
手足运动();
}
});
},[价值];
const handleOption=()=>{
如果(值==1){
getCheckValue1(真);
}
如果(值==2){
getCheckValue2(真);
}
};
返回(
{data.data}
약1.
handleClick(data.name,e.target.value)}
className=“选项1”
/>
handleClick(data.name,e.target.value)}
className=“选项2”
/>
);
};
导出默认的EditSurveyForm;
请帮帮我。谢谢。您的API的响应是什么样子的?您好,我来晚了。我从API中获取的数据如下:data.name=4,data.name=5。这取决于检查之前的值。数据类型是数字。