동적으로 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속성을 추가해주면 해결된다!
'Front > JS & jQuery' 카테고리의 다른 글
[jQuery] .attr() .prop() 차이 (0) | 2022.05.30 |
---|---|
[JS] 여러개의 값 리턴하기 (0) | 2022.05.27 |
[jQuery] .forEach() , .each() 반복문 (0) | 2022.05.25 |
[JS] 빈 배열로 초기화하기 (0) | 2022.05.23 |
[JS] .toFixed() - 소수점 아래 숫자 자릿수 제한하기 (0) | 2022.05.22 |