英文:
Unchecking Radio buttons is not working with React after useeffect
问题
以下是您表单中的翻译部分:
<Form>
<ListGroup>
<ListGroup.Item className="text-start">
<div>
<Form.Check
type="radio"
id="0"
name="answer"
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) => setSelectedAnswer(e.target.value)}
{...selectedAnswer != null ? "checked" : null}
/>
</div>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="1"
name="answer"
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={(e) => setSelectedAnswer(e.target.value)}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="2"
name="answer"
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={(e) => setSelectedAnswer(e.target.value)}
{...selectedAnswer != null ? "checked" : null}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="3"
name="answer"
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) => setSelectedAnswer(e.target.value)}
{...selectedAnswer != null ? "checked" : null}
/>
</ListGroup.Item>
</ListGroup>
<br />
<Button variant="danger" onClick={onClickPassRadio}>
submit
</Button>
</Form>
有关您的问题,首次提交后,您希望将所有单选按钮取消选择,但之后在 useEffect 中将 selectedAnswer 设置为 null 时,界面上显示为选中状态,尽管实际上它不应该选中。要解决此问题,您可以尝试以下更改:
const onClickPassRadio = (e) => {
e.preventDefault();
setSelectedAnswer("");
setNum(randomNumberInRange(10));
};
通过在 onClickPassRadio
函数中将 setSelectedAnswer
设置为空字符串,您可以确保提交后所有单选按钮都取消选择。这样,即使在 useEffect
中将 selectedAnswer
设置为 null,界面上也不会显示选中状态。
英文:
Below is my Form looks like in return section:
<Form >
<ListGroup >
<ListGroup.Item className="text-start" >
<div>
<Form.Check
type="radio"
id="0"
name="answer"
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={e => setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? "checked" : null}
/>
</div>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="1"
name="answer"
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={e => setSelectedAnswer(e.target.value)}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="2"
name="answer"
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={e => setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? "checked" : null}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="3"
name="answer"
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[0]}
onChange={e => setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? "checked" : null}
/>
</ListGroup.Item>
</ListGroup>
<br />
<Button variant="danger" onClick={onClickPassRadio}>submit</Button>
</Form>
What is happening is first time it works ie all the radio buttons are unchecked once Submit is pressed. But afterwards, useeffect is called and selectedAnswer is being set to null too. But even if I dont click any option, on UI it is shown as checked when it is not as useeffect afterwards shows value as null.
Is there a way to uncheck all the radio buttons once submit is pressed?
const [selectedAnswer, setSelectedAnswer] = useState("");
const [num, setNum] = useState(0);
const randomNumberInRange = ( max) => {
return Math.floor(Math.random()
* (max + 1));
};
const onClickPassRadio = (e) => {
e.preventDefault();
//console.log(selectedAnswer);
setNum(randomNumberInRange(10));
};
useEffect(() => {
setSelectedAnswer(null);
}, [setNum]);
答案1
得分: 1
<Form>
<ListGroup>
<ListGroup.Item className="text-start">
<div>
<Form.Check
type="radio"
id="0"
name="answer"
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[0]}
/>
</div>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="1"
name="answer"
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[1]}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="2"
name="answer"
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[2]}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="3"
name="answer"
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[3]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[3]}
/>
</ListGroup.Item>
</ListGroup>
<br />
<Button variant="danger" onClick={onClickPassRadio}>
submit
</Button>
</Form>
英文:
<Form>
<ListGroup>
<ListGroup.Item className="text-start">
<div>
<Form.Check
type="radio"
id="0"
name="answer"
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[0]}
/>
</div>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="1"
name="answer"
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[1]}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="2"
name="answer"
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[2]}
/>
</ListGroup.Item>
<ListGroup.Item className="text-start">
<Form.Check
type="radio"
id="3"
name="answer"
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) => setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[3]}
/>
</ListGroup.Item>
</ListGroup>
<br />
<Button variant="danger" onClick={onClickPassRadio}>
submit
</Button>
</Form>
答案2
得分: 0
为了在按下提交按钮后取消选中所有单选按钮,您需要将selectedAnswer状态更新为一个空字符串(""),而不是null。以下是应该实现所需行为的修改后的代码:
useEffect(() => {
setSelectedAnswer(""); // 将其更新为空字符串而不是null
}, [num]);
英文:
To uncheck all the radio buttons once the submit button is pressed, you need to update the selectedAnswer state to an empty string ("") instead of null. Here's the modified code that should achieve the desired behavior:
useEffect(() => {
setSelectedAnswer(""); // Update to an empty string instead of null
}, [num]);
答案3
得分: 0
希望这个对你有所帮助。
const onClickPassRadio = (e) => {
e.preventDefault();
setSelectedAnswer(""); // 重置selectedAnswer以取消选中所有单选按钮
setNum(randomNumberInRange(10));
};
英文:
Hope this one help you.
const onClickPassRadio = (e) => {
e.preventDefault();
setSelectedAnswer(""); // reset selectedAnswer to uncheck all radio buttons
setNum(randomNumberInRange(10));
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论