Home > Web Front-end > JS Tutorial > body text

How to delete options in select in jquery

青灯夜游
Release: 2020-12-21 17:39:27
Original
3983 people have browsed it

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

How to delete options in select in jquery

The operating environment of this tutorial: windows7 system, jquery3.3.1 version. This method is suitable for all brands of computers.

Recommended tutorial: jquery video tutorial

If you want to use jQuery to remove options (options elements) from the select element, you can first use the jQuery selector to select the required ones from the select Delete the options element and then use JQuery's remove() method to remove the option from the HTML document.

Example:

<!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

Rendering:

How to delete options in select in jquery

## 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.

For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of How to delete options in select 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!