jquery operation Select (value, set selected)
Each time operation Select, always come out to turn over the data, as a summary of their own, after the turn here.
Like <select class= "selector" ></select>
1, set the value of Pxx items selected
$ (". Selector"). Val ("Pxx");
2, set text to Pxx items selected
$ (". Selector"). Find ("option[text= ' Pxx ')"). attr ("selected", true);
Here is a use of brackets, preceded by an equal sign in brackets, with the attribute name, without quotes. In many cases, the use of brackets can make logic simple.
3, get the value of the currently selected item
$ (". Selector"). Val ();
4, get the text of the currently selected item
$ (". Selector"). Find ("option:selected"). Text ();
Using a colon, mastering its usage and extrapolate it makes the code concise.
Many times the cascade of select is used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.
Such as:
Copy Code code as follows:
$ (". Selector1"). Change (function () {
Empty the second one first.
$ (". Selector2"). empty ();
In practical applications, the option here is generally to generate multiple loops
var option = $ ("<option>"). Val (1). Text ("Pxx");
$ (". Selector2"). Append (option);
});
JS Operation Select Daquan
To determine if the item for value= "Paravalue" exists in the Select option
Add an item to the Select option
Remove an item from the Select option
Delete selected items in select
Modify Value= "Paravalue" in the Select option with the text "Paratext"
Set the first item in the Select Text= "Paratext" to select
Set value= "Paravalue" item to select in Select
Gets the value of the currently selected item of select
Gets the text of the currently selected item for select
Gets the index of the current selected item of select
Empty a Select item
JS Code
Copy Code code as follows:
1. Determine if there is a value= "Paravalue" Item in the Select option
function Jsselectisexititem (Objselect, Objitemvalue) {
var isexit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Isexit = true;
Break
}
}
return isexit;
}
2. Add an item to the Select option
function Jsadditemtoselect (Objselect, Objitemtext, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
Alert ("The value of the item already exists");
} else {
var varitem = new Option (Objitemtext, Objitemvalue);
ObjSelect.options.add (Varitem);
Alert ("Join successfully");
}
}
3. Remove an item from the Select option
function Jsremoveitemfromselect (Objselect, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
ObjSelect.options.remove (i);
Break
}
}
Alert ("Successful deletion");
} else {
Alert ("The item does not exist in the select");
}
}
4. Delete selected items in select
function Jsremoveselecteditemfromselect (objselect) {
var length = objselect.options.length-1;
for (var i = length; I >= 0; i--) {
if (objselect[i].selected = = True) {
Objselect.options[i] = null;
}
}
}
5. Modify the Select option Value= "Paravalue" text is "Paratext"
function Jsupdateitemtoselect (Objselect, Objitemtext, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Objselect.options[i].text = Objitemtext;
Break
}
}
Alert ("Successful modification");
} else {
Alert ("The item does not exist in the select");
}
}
6. Set the first item of text= "Paratext" in Select to select
function Jsselectitembyvalue (Objselect, Objitemtext) {
To determine whether there is
var isexit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].text = = Objitemtext) {
Objselect.options[i].selected = true;
Isexit = true;
Break
}
}
Show the results
if (isexit) {
Alert ("Successfully selected");
} else {
Alert ("The item does not exist in the select");
}
}
7. Set the item of value= "Paravalue" in Select to select
Document.all.objSelect.value = Objitemvalue;
8. Value of the current selected item of the Select
var currselectvalue = Document.all.objSelect.value;
9. Gets the text of the currently selected item of select
var currselecttext = Document.all.objselect.options[document.all.objselect.selectedindex].text;
10. Index of the current selected item for select
var currselectindex = Document.all.objSelect.selectedIndex;
11. Empty a Select item
document.all.objSelect.options.length = 0;