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

Làm cách nào để xóa các tùy chọn khỏi danh sách thả xuống bằng JavaScript?

Để xóa các tùy chọn khỏi danh sách thả xuống, hãy sử dụng phương thức remove () trong JavaScript. Bạn có thể thử chạy đoạn mã sau để tìm hiểu cách xóa các tùy chọn khỏi trình đơn 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 = "remove()" value = "Click to Remove">
      </form>
      <p>Select and click the button to remove the selected option.</p>
      
      <script>
         function remove() {
            var x = document.getElementById("selectNow");
            x.remove(x.selectedIndex);
         }
      </script>
      
   </body>
</html>