Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Xóa tùy chọn (option) khỏi danh sách thả xuống (dropdown) với JavaScript

Sử dụng phương thức remove() của đối tượng <select> để xóa option tại chỉ số chỉ định.

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>Chọn option và nhấn nút để xóa.</p>
    <script>
      function remove() {
        var x = document.getElementById("selectNow");
        x.remove(x.selectedIndex);
      }
    </script>
  </body>
</html>