Home  >  Article  >  Web Front-end  >  Implementation method of adding, deleting and modifying select options in jQuery

Implementation method of adding, deleting and modifying select options in jQuery

php中世界最好的语言
php中世界最好的语言Original
2018-04-23 16:44:141763browse

This time I will bring you jQueryHow to implement adding, deleting and modifying select options. What are the precautions for adding, deleting and modifying select options in jQuery? The following is a practical case. Let’s take a look. .

The example in this article describes how to add, delete and modify the select option in jQuery. Share it with everyone for your reference, the details are as follows:

jQuery gets the Text and Value selected by Select:

1.

//获取Select选择的Text
var checkText=jQuery("#select_id").find("option:selected").text();

2.

//获取Select选择的option Value
var checkValue=jQuery("#select_id").val();

3.

//获取Select选择的索引值
var checkIndex=jQuery("#select_id ").get(0).selectedIndex;

4.

//获取Select最大的索引值
var maxIndex=jQuery("#select_id option:last").attr("index");

jQuery adds/removes the Option item of Select:

1.

//为Select追加一个Option(下拉项)
jQuery("#select_id").append("<option value=&#39;Value&#39;>Text</option>");

2.

//为Select插入一个Option(第一个位置)
jQuery("#select_id").prepend("<option value=&#39;0&#39;>请选择</option>");

3.

//删除Select中索引值最大Option(最后一个)
jQuery("#select_id option:last").remove();

4.

//删除Select中索引值为0的Option(第一个)
jQuery("#select_id option[index='0']").remove();

5.

//删除Select中Value='3'的Option
jQuery("#select_id option[value='3']").remove();

6.

//删除Select中Text='4'的Option
jQuery("#select_id option[text='4']").remove();

Content clear:

jQuery("#select_id").empty();

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Summary of jquery’s time acquisition method

##What are the precautions for jQuery version upgrade

The above is the detailed content of Implementation method of adding, deleting and modifying select options in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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