Home > Web Front-end > JS Tutorial > A simple example of using js code to change the selected state of a radio button_javascript skills

A simple example of using js code to change the selected state of a radio button_javascript skills

WBOY
Release: 2016-05-16 17:08:22
Original
1531 people have browsed it

Today there was a sudden need to use js code to change the selected state of the radio button. At that time, I didn’t even think about it.

Copy the code The code is 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);
}


No response after execution...

Because we set checked="checked" in the radio tag; 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 to change to:

Copy the codeThe code is 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 bit confused... What went wrong? ? ? ?

Isn’t it the style attribute? ? ? ?

Click gel("radionan") directly to see the checked attribute.

That’s right! ! ! ! !

Copy code The code is as follows:

function doGender(gender) {
 if (gender == "male") {
 gel("radionan").checked = true;
 } else {
 gel("radionv").checked = true;
 }
}

Once executed, it was indeed the case. . . . . . . . . . . . . .

That’s it! ! ! ! ! ! ! ! ! ! ! !

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template