본문 바로가기
FrontEnd/jQuery

[jQuery] .prop의 사용 예 : disabled속성 해제 or 부여 / checked 해제 or 부여 / selected해제 or 부여

by 성은2 2021. 6. 4.

disabled, checked 속성은 불리언(boolean) 속성입니다.

- 불리언 속성은 해당 속성을 명시하면 자동으로 true 값을 가지게 됩니다.

- 명시하지 않으면 속성값이 자동으로 false 값을 가지게 됩니다.

- 속성이 존재한다면 해당 속성이 true임을 의미합니다

 

$('#jobName').prop('disabled', true); // 해당 요소에 disabled 속성 주기

$('input[name=dataChk]:checked').prop('checked', false); // 해당 요소의 checked된 속성 해제 하기

$('#selectYear').prop('selected', true); // 해당 요소 selected 속성 주기

 

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checkedselected, or disabled state of form elements, use the .prop() method.

 

 

According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false". This is true of all boolean attributes.

(W3C 양식 사양에 따르면 체크된 특성은 부울 특성으로, 속성이 존재한다면 해당 속성이 참임을 의미합니다. 예를 들어 속성이 값이 없거나 빈 문자열 값으로 설정된 경우에도 마찬가지입니다. 이는 모든 부울 특성에 해당됩니다.)