Input Radio checked Attribute

Definition and Usage

checked Sets or returns the selected state of the radio button.

This attribute reflects the HTML checked attribute.

See also:

HTML Reference Manual:HTML <input> checked 属性

Example

Example 1

Select and deselect a specified radio button:

function check() {
  document.getElementById("red").checked = true;
}
function uncheck() {
  document.getElementById("red").checked = false;
}

Try it yourself

Example 2

Check if the radio button is selected:

var x = document.getElementById("myRadio").checked;

Try it yourself

Example 3

Use radio buttons to convert text in input fields to uppercase:

document.getElementById("fname").value = document.getElementById("fname").value.toUpperCase();

Try it yourself

Example 4

Radio buttons in the form:

var coffee = document.forms[0];
var txt = "";
var i;
for (i = 0; i < coffee.length; i++) {
  if (coffee[i].checked) {
    txt = txt + coffee[i].value + " ";
  }
}
document.getElementById("order").value = "You ordered coffee with: " + txt;

Try it yourself

Syntax

Return the checked attribute:

radioObject.checked

Set the checked attribute:

radioObject.checked = true|false

属性值

描述
true|false

规定是否应选中单选按钮。

  • true - 单选按钮被选中
  • false - 默认。未选中单选按钮

技术细节

返回值: 布尔值,如果单选按钮被选中则返回 true;否则返回 false

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持