Computer >> Máy Tính >  >> Lập trình >> Javascript

Làm cách nào để hiển thị tùy chọn đã chọn trong danh sách thả xuống bằng JavaScript?


Để hiển thị tùy chọn đã chọn trong danh sách thả xuống bằng JavaScript, bạn có thể thử chạy đoạn mã sau. Điều này cho phép người dùng nhận giá trị mà họ đã chọn từ danh sách thả xuống.

Ví dụ

<!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</p>
      <script>
         function display() {
            var obj = document.getElementById("selectNow");
            document.write(obj.options[obj.selectedIndex].text);
         }
      </script>
   </body>
</html>