JS Operation Select Daquan (value, set selection, etc.)

Source: Internet
Author: User

jquery operation Select (value, set selected)

Every time the operation of the Select, always to come out to turn over the information, as a summary of their own, after the turn over here.

such as <select class= "selector" ></select>

1. Set value to Pxx item selected

$ (". Selector"). Val ("Pxx");

2. Set text to Pxx to select

$ (". Selector"). Find ("option[text= ' Pxx ')"). attr ("selected", true);

Here is the use of a bracket, preceded by the attribute name in parentheses, without quotation marks. In many cases, the use of brackets can make logic very simple.

3. Gets the value of the currently selected item

$ (". Selector"). Val ();

4. Gets the text of the currently selected item

$ (". Selector"). Find ("option:selected"). Text ();

Using a colon here, mastering its usage and extrapolate will also make the code concise.

There are many times when a select cascade is used, that is, the value of the second select changes with the value selected in the first select. This is very simple in jquery.

Such as:

  $ ( ". Selector1"). Change (function  Span style= "COLOR: #000000" > () { //  Empty the second one first  $ ( ". Selector2"  //  In practical applications, the option here is typically generated by looping multiple  var  option = $ ("<option>"). Val (1). Text ("Pxx"  ". Selector2" 

JS Operation Select Daquan

Determine if the item 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 the text of value= "Paravalue" in the Select option to "Paratext"
Set the first item of text= "Paratext" in Select to be selected
Set the item for value= "Paravalue" in Select to be selected
Gets the value of the currently selected item of select
Gets the text of the current selected item of select
Gets the index of the current selected item of select
Empty the Select item
JS Code

 //1. Determine if the value= "Paravalue" item exists in the SELECT optionfunctionJsselectisexititem (Objselect, objitemvalue) {varIsexit =false;  for(vari = 0; i < objSelect.options.length; i++) { if(Objselect.options[i].value = =objitemvalue) {Isexit=true;  Break; } } returnIsexit;} //2. Add an item to the Select optionfunctionJsadditemtoselect (Objselect, Objitemtext, objitemvalue) {//determine if there isif(Jsselectisexititem (Objselect, Objitemvalue)) {alert ("The value of the item already exists"); } Else { varVaritem =NewOption (Objitemtext, Objitemvalue); ObjSelect.options.add (varitem); alert ("Join successfully"); } } //3. Remove an item from the Select optionfunctionJsremoveitemfromselect (Objselect, objitemvalue) {//determine if there isif(Jsselectisexititem (Objselect, Objitemvalue)) { for(vari = 0; i < objSelect.options.length; i++) { if(Objselect.options[i].value = =objitemvalue) {ObjSelect.options.remove (i); Break; } } alert ("Successfully deleted"); } Else{alert ("The item does not exist in this select"); } } //4. Delete the selected item in selectfunctionJsremoveselecteditemfromselect (objselect) {varLength = Objselect.options.length-1;  for(vari = length; I >= 0; i--){ if(objselect[i].selected = =true) {Objselect.options[i]=NULL; } } } //5. Modify the text of value= "Paravalue" in the Select option to "Paratext"functionJsupdateitemtoselect (Objselect, Objitemtext, objitemvalue) {//determine if there isif(Jsselectisexititem (Objselect, Objitemvalue)) { for(vari = 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 this select"); } } //6. Set the first item of text= "Paratext" in Select to be selectedfunctionJsselectitembyvalue (Objselect, objitemtext) {//determine if there isvarIsexit =false;  for(vari = 0; i < objSelect.options.length; i++) { if(Objselect.options[i].text = =Objitemtext) {objselect.options[i].selected=true; Isexit=true;  Break; } } //Show out Resultsif(isexit) {alert ("Selected successfully"); } Else{alert ("The item does not exist in this select"); } } //7. Set the item of value= "Paravalue" in Select to selectDocument.all.objSelect.value =Objitemvalue;//8. Gets the value of the current selected item of selectvarCurrselectvalue =Document.all.objSelect.value;//9. Gets the text of the current selected item of selectvarCurrselecttext =Document.all.objselect.options[document.all.objselect.selectedindex].text;//10. Get the index of the current selected item of selectvarCurrselectindex =Document.all.objSelect.selectedIndex;//11. Clear the Select itemdocument.all.objSelect.options.length = 0;

JS Operation Select Daquan (value, set selection, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.