<input type="checkbox" name="test" value="1" /> 1
<br />
<input type="checkbox" name="test" value="2" /> 2
<br />
<input type="checkbox" name="test" value="3" /> 3
<br />
const checkOnlyOne = (checkThis) => {
const checkboxes = document.getElementsByName('test')
for (let i = 0; i < checkboxes.length; i++) {
if (checkboxes[i] !== checkThis) {
checkboxes[i].checked = false
}
}
}
<input type="checkbox" name="test" value="1" onChange={(e) => checkOnlyOne(e.target)} /> 1
<br />
<input type="checkbox" name="test" value="2" onChange={(e) => checkOnlyOne(e.target)} /> 2
<br />
<input type="checkbox" name="test" value="3" onChange={(e) => checkOnlyOne(e.target)} /> 3
<br />
export const CheckBoxOne = () => {
const checkOnlyOne = (checkThis) => {
const checkboxes = document.getElementsByName('test')
for (let i = 0; i < checkboxes.length; i++) {
if (checkboxes[i] !== checkThis) {
checkboxes[i].checked = false
}
}
}
return (
<>
<input type="checkbox" name="test" value="1" onChange={(e) => checkOnlyOne(e.target)} /> 1
<br />
<input type="checkbox" name="test" value="2" onChange={(e) => checkOnlyOne(e.target)} /> 2
<br />
<input type="checkbox" name="test" value="3" onChange={(e) => checkOnlyOne(e.target)} /> 3
<br />
</>
)
}
해당 내용은 다음 자료를 참고했습니다.
https://itpak.tistory.com/210