The help house (www.bkjia.com) Tutorial Project encountered the need to use javascript to operate the drop-down box, by the way, make a summary, list some common methods, the following methods have been tested on FIRFOX3.5 and IE8, if other browsers cannot run properly, contact the author.
Reference content is as follows:
// Add a drop-down list
Function AddDropDownList (id, fatherCtl)
{
If (! Document. getElementById (id ))
{
Var ddl = document. createElement ('select ');
Ddl. setAttribute ("id", id );
If (fatherCtl & document. getElementById (fatherCtl ))
Document. getElementById (fatherCtl). appendChild (ddl );
Else
Document. body. appendChild (ddl );
}
}
// Delete the specified drop-down list
Function RemoveDropDownList (id)
{
Var ctl = document. getElementById (id );
If (ctl)
Ctl. parentNode. removeChild (ctl );
}
// Add options to the drop-down list
Function AddDDDLOption (id, text, value)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Ctl. options [ctl. options. length] = new Option (text, value );
}
}
// Delete all options
Function RemoveAllDDLOptions (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Ctl. options. length = 0;
}
}
// Delete the option of the specified index
Function RemoveDDLOption (id, index)
{
Var ctl = document. getElementById (id );
If (ctl & ctl. options [index])
{
Ctl. options [index] = null;
}
}
// Obtain the value selected from the drop-down list
Function GetDDLSelectedValue (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Return ctl. options [ctl. selectedIndex]. value;
}
}
// Obtain the selected text from the drop-down list
Function GetDDLSelectedText (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Return ctl. options [ctl. selectedIndex]. text;
}
} |
From: http://www.cnblogs.com/liuwu/ Author: Liu Wu