Escape from the traditional four kinds of operation: increase, delete, change, check.
< ransacked jquery's code for select operations, pooling them for later viewing. Calendar days change code for original. >
[Add]:
Copy Code code as follows:
$ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)
$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
[Delete]:
Copy Code code as follows:
$ ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select
$ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value of 0 in select (first)
$ ("#select_id option[value= ' 3 ']"). Remove (); Delete option value= ' 3 ' in select
$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select
$ ("#select_id"). empty (); Empty
[Change] ( set selected):
Copy Code code as follows:
$ ("#select_id"). Get (0). selectedindex=1; To set the Select index value of 1
$ ("#select_id"). Val (4); Select to set the value of 4 for the Select
$ ("#select_id option[text= ' jQuery ']"). attr ("selected", true); Set the text value of select to the item selected for jquery
[Check] ( get the selected value):
1. Get the value of the selected item
Copy Code code as follows:
$ ("#select_id"). Val (); Gets the value of the selected item
$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the selected item
$ ("#select_id"). Find ("option:selected"). Text (); Gets the text of the selected item
$ ("#select_id option:last"). attr ("index"); Get the maximum index value for select
Enclosed is a code sample that changes the number of days in the Select day by selecting "year" and "month" based on the actual selection.
Written using PHP, the default $ ("Select.day") is initially 31 days, because the default is January:
Copy Code code as follows:
<select name = "Date_year" class= "Year" >//Years
<?php
for ($year = 1990; $year <= Date ("Y"); + + $year) {
?>
<option value= "<?php echo $year;? > "><?php echo $year;? ></option>
<?php
}
?>
</select>
Copy Code code as follows:
<select name = "Date_month" class= "month" >//month
<?php
for ($month = 1; $month <= + + $month) {
?>
<option value= "<?php echo $month;? > "><?php echo $month;? ></option>
<?php
}
?>
</select>
Copy Code code as follows:
<select name = "Date_day" class= "Day" >//Days
<?php
for ($day = 1; $day <= + + $day) {
?>
<option value= "<?php echo $day;? > "><?php echo $day;? ></option>
<?php
}
?>
</select>
jquery Code:
Copy Code code as follows:
$ (document). Ready (function () {
$ ("Select.month, Select.year"). Change (function () {//"year", "month" changes can cause "day" change
$ ("Select.day"). empty (); It's very important to clear it first.
for (var i = 1; i < i++) {
var option = $ ("<option>"). Val (i). text (i);
$ ("Select.day"). Append (option);
}
var month = $ ("Select.month"). Val ();
if ((Month% 2 && month < 8) | | (month% 2 = = 0 && month > 7)) {
$ ("Select.day"). Append ("<option value= ' >31</option>"); Days are 31 days append an option
}
if (month = = 2) {
$ ("Select.day option:last"). Remove ();
$ ("Select.day option:last"). Remove (); February Days 28
var year = $ ("Select.year"). Val ();
if (year% 4 = 0 && year%) | |-year% 400 = 0)
$ ("Select.day"). Append ("<option value= ' >29</option>"); Leap year February days plus One
}
});
});
The above is about jquery to select the increase, delete, change, check operation summary, I hope we can enjoy.