JS operation select Element

Source: Internet
Author: User

 

/* ------------------------------------------------------
* Description: Common Operations of select element Javascript
* 1. determine whether an item with the specified value exists
* 2. Add an item
* 3. Delete all items whose values are Value
* 4. Option for deleting an index
* 5. Update the value and text of the index.
* 6. Set the first item of the specified text in the SELECT statement to be selected.
* 7. Set the first item of the specified value in the SELECT statement to be selected.
* 8. Obtain the value of the selected item.
* 9. Obtain the index of the selected item.
* 10. Get the text of the selected item.
* 11. Clear all options
------------------------------------------------------- */
// 1. determine whether an item with the specified value exists
Function Existvalue (OBJ, value ){
For ( VaR I = 0 ; I < OBJ. Options. length; I ++ ){
If (Obj. Options [I]. Value = Value ){
Return   True ;
}
}
Return   False ;
}
// 2. Add an item
Function Additem (OBJ, text, value ){
  VaR Varitem =   New Option (text, value );
OBJ. Options. Add (varitem );
}
// 3. Delete all items with a value
Function Removeitems (OBJ, value ){
  For ( VaR I = 0 ; I < OBJ. Options. length; I ++ ){
If (Obj. Options [I]. Value = Value ){
OBJ. Remove (I );
}
}
}
// 4. Option for deleting an index
Function Removeitem (OBJ, index ){
OBJ. Remove (INDEX );
}

// 5. Update the value and text of the index.
Function Updateitem (OBJ, index, value, text ){
OBJ. Options [Index]. Value = Value;
OBJ. Options [Index]. Text = Text;
}

// 6. Set the first item of the specified text in the SELECT statement to be selected.
Function Selectitembytext (OBJ, text ){
VaR Isexit =   False ;
For ( VaR I = 0 ; I < OBJ. Options. length; I ++ ){
If (Obj. Options [I]. Text = Text ){
OBJ. Options [I]. Selected =   True ;
Return   True ;
}
}
  Return   False ;
 
}
// 7. Set the first item of the specified value in the SELECT statement to be selected.
Function Selectitembyvalue (OBJ, value ){
VaR Isexit =   False ;
For ( VaR I = 0 ; I < OBJ. Options. length; I ++ ){
If (Obj. Options [I]. Value = Value ){
OBJ. Options [I]. Selected =   True ;
Return   True ;
}
}
  Return   False ;
 
}
// 8. Obtain the value, index, and text of the selected item.
Function Getvalue (OBJ ){
  Return OBJ. value;
}
// 9. Obtain the index of the selected item.
Function Getindex (OBJ ){
  Return OBJ. selectedindex;
}
// 10. Get the text of the selected item.
Function Gettext (OBJ ){
  Return OBJ. Options [obj. selectedindex]. text;
}
// 11. Clear all options
Function Clear (OBJ ){
OBJ. Options. Length =   0 ;
}

 

. Net assigns Default ValueCode

 

 

Dropdownlist1.selectedvalue = String;
// String is the value selected by dropdownlist when you first enter the page.
// The following can be set based on text and value.
Dropdownlist1.items. findbytext (string). Selected = True ;
Dropdownlist1.items. findbyvalue (string). Selected = True ;

 

 

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.