Today there is a sudden need to use js code to change the selected state of a radio button. At that time, I didn’t even think about it directly.
Copy the code code as follows:
function doGender(gender) {
if (gender == "male") {
gel("radionan").style.checked = "checked";
} else {
gel("radionv").style.checked = "checked";
}
}
function gel(id) {
return document.getElementById(id);
}
Once executed, there is no response...
Because we set the selection in the radio tag to checked="checked"; so we subconsciously assigned the value of gel("radionv").style.checked to "checked", and then checked online.
It turns out that to select the radio button in the js code, you need to assign the value of checked to true.
Continue changing to:
Copy the code code as follows:
function doGender(gender) {
if (gender == "male") {
gel("radionan").style.checked = true;
} else {
gel("radionv").style.checked = true;
}
}
As soon as I executed it, there was still no response. I was a little confused... What went wrong? ? ? ?
Isn't it a style attribute? ? ? ?
Just click gel("radionan") to see the checked attribute.
That’s right! ! ! ! !
Copy the code code as follows:
function doGender(gender) {
if (gender == "male") {
gel("radionan").checked = true;
} else {
gel("radionv").checked = true;
}
}
Once executed, it is indeed the case. . . . . . . . . . . . . .
That's the end of it! ! ! ! ! ! ! ! ! ! ! !