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