Computer >> 컴퓨터 >  >> 프로그램 작성 >> HTML

HTML DOM 입력 라디오 확인 속성

<시간/>

HTML DOM 입력 라디오 선택 속성은 라디오 유형을 가진 요소의 checked 속성과 연결됩니다. 기본적으로 라디오 버튼의 체크된 속성 값을 설정하거나 반환하는데 사용합니다.

구문

다음은 −

의 구문입니다.

체크된 속성을 설정합니다.

radioObject.checked = true|false

예시

Radio checked 속성의 예를 살펴보겠습니다.

<!DOCTYPE html>
<html>
<body>
<h1>Input Radio checked property</h1>
<form>
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
<input type="radio" name="fruits" id="Apple">Apple
</form>
<br><button type=”button” onclick="checkApple()">Check Apple</button>
<script>
   function checkApple() {
      document.getElementById("Apple").checked = true;
   }
</script>
</body>
</html>

출력

이것은 다음과 같은 출력을 생성합니다 -

HTML DOM 입력 라디오 확인 속성

"Apple 확인" 버튼을 클릭하면 -

HTML DOM 입력 라디오 확인 속성

위의 예에서 -

공통 속성 type="radio" 및 name="fruits"를 갖는 양식 내부에 두 개의 입력 필드가 있습니다. 첫 번째 라디오 버튼의 id는 "Mango"이고 두 번째 라디오 버튼의 id는 "Apple"입니다. -

<form>
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
<input type="radio" name="fruits" id="Apple">Apple
</form>

그런 다음 사용자가 클릭할 때 checkApple() 메서드를 실행하는 "Apple 확인" 버튼을 만들었습니다.

<button type=”button” onclick="checkApple()">Check Apple</button>

checkApple() 메서드는 getElementById() 메서드를 사용하여 라디오 유형의 입력 요소를 가져오고 해당 checked 속성을 true로 설정합니다. 이것은 사과 라디오 버튼을 확인합니다:−

function checkApple() {
   document.getElementById("Apple").checked = true;
}