Home > Web Front-end > JS Tutorial > How to delete select option in jQuery?

How to delete select option in jQuery?

青灯夜游
Release: 2020-11-30 15:14:30
Original
3816 people have browsed it

Deletion method: First use the jQuery selector to select the options element option that needs to be deleted from the select, and then use JQuery's r [remove()] method to delete the option from the HTML document, the syntax [$( selector).remove()].

How to delete select option in jQuery?

Related recommendations: "jq tutorial"

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, This method works for all brands of computers.

Example of jQuery deleting select option

<!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>       
        $(&#39;button&#39;).on(&#39;click&#39;, function() { 
            $("option[value=&#39;val_1&#39;]").remove(); 
            $(&#39;#Text&#39;).text(&#39;值为val_1的选项已删除!&#39;); 
        }); 
    </script> 
</body> 
  
</html>
Copy after login

The remove() method is used to remove selected elements, including all text and child nodes. This method also removes data and events from the selected element.

Grammar

$(selector).remove()
Copy after login

For more programming-related knowledge, please visit: Programming Course! !

The above is the detailed content of How to delete select option in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template