Home  >  Article  >  Web Front-end  >  Jquery's add, delete, modify, and check operations on select_jquery

Jquery's add, delete, modify, and check operations on select_jquery

WBOY
WBOYOriginal
2016-05-16 16:15:12916browse

You can’t escape the traditional four operations: add, delete, modify, and check.

[Added]:

Copy code The code is as follows:

$("#select_id").append(""); //Append an Option (drop-down item) to Select
$("#select_id").prepend(""); //Insert an Option (first position) for Select

[Deleted]:

Copy code The code is as follows:

$("#select_id option:last").remove(); //Delete the Option with the largest index value in Select (the last one)
$("#select_id option[index='0']").remove(); //Delete the Option (first) with index value 0 in Select
$("#select_id option[value='3']").remove(); //Delete the Option with Value='3' in Select
$("#select_id option[text='4']").remove(); //Delete the Option with Text='4' in Select
$("#select_id").empty(); //Empty

[Change](Set selected items):

Copy code The code is as follows:

$("#select_id ").get(0).selectedIndex=1; //Set the item with Select index value 1 to select
$("#select_id ").val(4); //Set the Select value to 4 to select the item
$("#select_id option[text='jQuery']").attr("selected", true); //Set the Text value of Select to the jQuery item selected

[Check](Get the selected value):

1. Get the value of the selected item

Copy code The code is as follows:

$("#select_id").val(); //Get the value of the selected item
$("#select_id ").get(0).selectedIndex; //Get the index value of the selected item
$("#select_id").find("option:selected").text(); //Get the text of the selected item
$("#select_id option:last").attr("index"); //Get the maximum index value of Select

A code sample is attached. The code function is to change the number of days in the option "Day" according to the actual selected "Year" and "Month".

Written in php, the default $("select.day") initially has 31 days, because the default is January:

Copy code The code is as follows:


Copy code The code is as follows:


Copy code The code is as follows:


jquery code:

Copy code The code is as follows:

$(document).ready(function() {
$("select.month, select.year").change(function() { //Changes in "year" and "month" may cause changes in "day"
            $("select.day").empty(); //Very important, clear it first
for (var i = 1; i < 31; i ) {
               var option = $(""); //Append an option
if the number of days is 31 days }
If (month === 2) {
                $("select.day option:last").remove();
                $("select.day option:last").remove(); //The number of days in February is 28
          var year = $("select.year").val();
If ((year % 4 === 0 && year % 100) || year % 400 === 0)
                   $("select.day").append(""); //Add one to the number of days in February in leap years
}
});
});

The above is a summary of jQuery’s implementation of adding, deleting, modifying, and checking select operations. I hope you all like it.

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