Development Tip

jQuery로 선택한 옵션 ID 가져 오기

yourdevel 2020. 10. 22. 22:54
반응형

jQuery로 선택한 옵션 ID 가져 오기


jQuery를 사용하여 선택한 옵션을 기반으로 ajax 요청을 만들려고합니다.

jQuery를 사용하여 선택한 옵션 ID (예 : "id2") 를 검색하는 간단한 방법이 있습니까?

<select id="my_select">
   <option value="o1" id="id1">Option1</option>
   <option value="o2" id="id2">Option2</option>
</select>


$("#my_select").change(function() {
    //do something with the id of the selected option
});

다음 과 같이 :selected선택기를 사용하여 가져올 수 있습니다 .

$("#my_select").change(function() {
  var id = $(this).children(":selected").attr("id");
});

var id = $(this).find('option:selected').attr('id');

그런 다음 원하는 것을 selectedIndex

나는 내 대답을 다시 편집했다 ... selectedIndex는 예제를 제공하기에 좋은 변수가 아니기 때문에 ...


$('#my_select option:selected').attr('id');

가장 쉬운 방법은 var id = $ (this) .val (); 변화와 같은 이벤트 내부에서.

참고 URL : https://stackoverflow.com/questions/2888446/get-the-selected-option-id-with-jquery

반응형

'Development Tip' 카테고리의 다른 글

iPad : PNG 크러시 오류  (0) 2020.10.22
Git CLI : 사용자 이름에서 사용자 정보 가져 오기  (0) 2020.10.22
Are FP and OO orthogonal?  (0) 2020.10.21
Rounded table corners CSS only  (0) 2020.10.21
data type not understood  (0) 2020.10.21