How to remove specified option in jquery

PHPz
Release: 2023-05-23 15:22:37
Original
1513 people have browsed it

jQuery is a commonly used JavaScript library that can easily manipulate DOM and handle events. When developing web applications, you often need to operate drop-down boxes, such as adding, deleting, and modifying options. For the removal option, we can use jQuery's remove() method. This article details how to remove a specified option from a drop-down box using jQuery.

1. How to obtain the drop-down box options

To delete the options in the drop-down box, you first need to obtain the corresponding options. We can use jQuery's find() method to get all the options in the drop-down box. The code is as follows:

var options = $('#select-id').find('option');
Copy after login

Among them, #select-id is the ID of the drop-down box, option is jQuery's built-in selector, which represents the option element. The above code will return a jQuery object containing all the options in the drop-down box.

If you need to get specific options, you can use jQuery's eq() method to specify the option index. For example, to get the third option in the drop-down box, the code is as follows:

var option = $('#select-id').find('option').eq(2);
Copy after login

Among them, the parameter of the eq() method represents the index of the option, counting from 0.

2. How to delete options

After obtaining the option element, you can use jQuery's remove() method to delete the option, as shown below:

option.remove();
Copy after login

In this way, you can delete the specified option.

If you need to remove multiple options, you can use jQuery's each() method to traverse the option list and delete them one by one. The sample code is as follows:

var options = $('#select-id').find('option');
options.each(function() {
  if($(this).val() == 'value1' || $(this).val() == 'value2') {
    $(this).remove();
  }
});
Copy after login

The above code will delete the value in the drop-down box. Options for value1 or value2.

3. Example Demonstration

The following is a complete demonstration example that demonstrates how to delete the specified option in the drop-down box through a button click event:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery 删除下拉框选项</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
  <select id="fruit">
    <option value="">请选择水果</option>
    <option value="apple">苹果</option>
    <option value="banana">香蕉</option>
    <option value="orange">橙子</option>
    <option value="watermelon">西瓜</option>
  </select>
  <button id="remove-btn">删除水果</button>
  <script>
    $(function() {
      $('#remove-btn').click(function() {
        var options = $('#fruit').find('option');
        options.each(function() {
          if($(this).val() == 'banana' || $(this).val() == 'watermelon') {
            $(this).remove();
          }
        });
      });
    });
  </script>
</body>
</html>
Copy after login

In the above example, We created a dropdown box with five options and a delete button. When the button is clicked, the code will remove the option with the value banana or watermelon. You can view the demonstration effect by running the above code.

Summary

jQuery is a very powerful JavaScript library that can easily manipulate DOM and handle events. For operations such as adding, deleting, and modifying options in drop-down boxes, jQuery's API provides many convenient methods that developers can use flexibly. This article details how to remove a specified option from a drop-down box using jQuery. Hope this helps!

The above is the detailed content of How to remove specified option in jquery. For more information, please follow other related articles on the PHP Chinese website!

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