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