Front/JS & jQuery

[jQuery] [error]동적으로 selectbox selected옵션 추가할 때 에러

오선지♬ 2022. 5. 26. 19:11
728x90
반응형

동적으로 selectbox를 생성하고 선택하는 것에 대한 게시글은 이미 포스팅 해둔 것이 있다.

https://imswengineer.tistory.com/144

 

[jQuery] 동적으로 selectbox만들기 , selected 속성 추가하기

⭐동적으로 selectbox 만들기 result.result.forEach((code)=>{ $('#' + formId) .append( $(' ', { value : code['CODE_ID'] }).text(code['CODE_NM']) ); if(code.CODE_ID == "3"){ $('#' + formId).find('optio..

imswengineer.tistory.com

 

이것을 프로젝트에서 사용하다가 selected옵션의 checked되는 요소가 달라질때 작동을 안하는 에러를 겪게되었다.

원인은 이미선택된 요소의 selected 속성을 제거해주고 새로 selected속성을 추가해줘야 하는 문제였다.

https://www.codingfactory.net/10200

 

jQuery / Method / .removeAttr() - 선택한 요소의 특정 속성을 제거하는 메서드

.removeAttr() .removeAttr()은 선택한 요소의 특정 속성을 제거합니다. 문법 .removeAttr( attributeName ) 예를 들어 $( 'h1' ).removeAttr( 'title' ); 은 h1 요소에서 title 속성을 제거합니다. 예제 input 요소의 placeholder

www.codingfactory.net

.removeAttr()을 사용하면 된다.

 

$('#id값').removeAttr('selected');

해주고 다시 selected속성을 추가해주면 해결된다!

728x90
반응형