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

JavaScript를 사용하여 드롭다운 목록에 선택한 옵션의 인덱스를 표시하는 방법은 무엇입니까?


드롭다운 목록에서 선택한 옵션의 색인을 표시하려면 selectedIndex JavaScript의 속성

예시

선택한 옵션의 색인을 표시하기 위해 다음 코드를 실행할 수 있습니다 -

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm">
         <select id="selectNow">
            <option>One</option>
            <option>Two</option>
            <option>Three</option>
          </select>
          <input type="button" onclick="display()" value="Click">
      </form>
      <p>Select and click the button to get the index of the selected option.</p>
      <script>
         function display() {
            var index = document.getElementById("selectNow").selectedIndex;
            document.write(index);
         }
      </script>
   </body>
</html>