简体   繁体   中英

How to check which radio button has been selected in javascript?

If (acctRB.Checked == true)

{
Execute Business Code
}

Well, you don't need the == true :

if (acctRB.checked)
{
    // It's checked
} 
else 
{
    // It's not checked
}

So what you have is pretty much correct. Just remove the == true as it's not required.

Bear in mind that IE is case insensitive while other browsers aren't. Your sample code will work for IE....

Rather do

if (acctRB.checked) {
     //Checked
} else {

  //Unchecked
}

It works for both IE and other major browsers....Or, if if checkbox id is "acctRB" do,

if (document.getElementById("acctRB").checked) {

   //Checked
} else {

   //Unchecked
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM