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

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

php中世界最好的语言
Release: 2018-04-23 16:44:14
Original
1812 people have browsed it

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();
Copy after login

2.

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

3.

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

4.

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

jQuery adds/removes the Option item of Select:

1.

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

2.

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

3.

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

4.

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

5.

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

6.

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

Content clear:

jQuery("#select_id").empty();
Copy after login

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!

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!