jQuery怎麼刪除select選項?

2020-11-24 15:01:17

刪除方法:首先使用jQuery選擇器從select中選擇需要刪除的options元素選項,然後使用JQuery的remove()方法從HTML檔案中刪除該選項即可,語法「$(selector).remove()」。

相關推薦:《》

jQuery刪除select選項的範例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
 
<body style="text-align:center;" id="body"> 
    <p style="font-size: 15px; font-weight: bold;">單擊按鈕,從select選擇框中刪除選項</p> 
    <select> 
        <option value="val_1"> Val_1</option> 
        <option value="val_2"> Val_2</option> 
        <option value="val_3"> Val_3</option> 
        <option value="val_4"> Val_4</option> 
    </select> 
    <br> 
    <br> 
    <button> 單擊 </button> 
    <p id="Text" style="color: green;font-size: 24px;font-weight: bold;"></p> 
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
    <script>       
        $('button').on('click', function() { 
            $("option[value='val_1']").remove(); 
            $('#Text').text('值為val_1的選項已刪除!'); 
        }); 
    </script> 
</body> 
  
</html>

remove()方法用於法移除被選元素,包括所有的文字和子節點。該方法也會移除被選元素的資料和事件。

語法

$(selector).remove()

更多程式設計相關知識,請存取:!!

以上就是jQuery怎麼刪除select選項?的詳細內容,更多請關注TW511.COM其它相關文章!