[Basic JavaScript control operations]-Select operations

Source: Internet
Author: User

Select operations:

1. verify the existence ---> isexistvalue (selectobj, value );
2. register a new item ---> addnewitem (selectobj, selecttext, selectvalue );
3. Remove items ---> deleteitem (selectobj, selectvalue );
4. Delete the selected item ---> deleteselecteditem (selectobj );
5. Set item to the first item ---> setselectedfirst (selectobj, value );
6. Set item as the selected item ---> setselecteditem (selectobj, value );
7. Clear Select ---> clearselect (selectobj );

Detailed description:

Selectobj is the select control object var selectobj = Document. getelementbyid ('select1 ');

1. verify the existence
Function isexistvalue (selectobj, value)
{
VaR isexist = false;
For (VAR I = 0; I <selectobj. Options. length; I ++)
{
If (selectobj. Options [I]. value = value)
{
Isexist = true;
}
}
Return isexist;
}

2. register a new project
Function addnewitem (selectobj, selecttext, selectvalue)
{
If (isexistvalue (selectobj, selectvalue ))
{
Alert ("existing value:" + selectvalue );
Return false;
}
VaR opt = New Option (selecttext, selectvalue)
Selectobj. Options. Add (OPT)
}

3. Remove items
Function deleteitem (selectobj, selectvalue)
{
If (! Isexistvalue (selectobj, selectvalue ))
{
Alert ("This item does not exist! ");
Return false;
}
For (VAR I = 0; I <selectobj. Options. length; I ++)
{
If (selectobj. Options [I]. value = selectvalue)
Selectobj. Options. Remove (I );
}
}

4. Delete selected items
Function deleteselecteditem (selectobj)
{
If (selectobj. Options. Length = 0)
{
Alert ("no data! ");
Return false;
}
VaR Index = selectobj. Options. selectedindex;
Var val = selectobj. Options [Index]. value;
If (isexistvalue (selectobj, Val ))
{
Alert ("deleted successfully:" + val );
Selectobj. Options. Remove (INDEX );
}
Else
{
Alert ("not found! ");
}
}
5. Set item to the first item.
Function setselectedfirst (selectobj, value)
{
If (! Isexistvalue (selectobj, value )){
Alert ("This item does not exist! ");
Return false;
}
VaR Len = selectobj. Options. length;
For (VAR I = 0; I <Len; I ++)
{
If (selectobj. Options [I]. value = value)
{
Selectobj. Options [I]. Selected = true;
Break;
}
}
}

6. Set item as the selected item
Function setselecteditem (selectobj, value)
{
If (! Isexistvalue (selectobj, value )){
Alert ("This item does not exist! ");
Return false;
}
VaR Len = selectobj. Options. length;
For (VAR I = 0; I <Len; I ++)
{
If (selectobj. Options [I]. value = value)
{
Selectobj. textvalue = value;
Break;
}
}
}

7. Clear select
Function clearselect (selectobj)
{
Selectobj. Options. Length = 0;
}

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.