1
检测是否有选中
if (objSelect.selectedIndex > - 1 ){
// 说明选中
} else {
// 说明没有选中
}
2 删除被选中的项
objSelect.options[objSelect.selectedIndex] = null ;
3 增加项
objSelect.options[objSelect.length] = new Option( " 你好 " , " hello " );
4 修改所选择中的项
objSelect.options[objSelect.selectedIndex] = new Option( " 你好 " , " hello " );
5 得到所选择项的文本
objSelect.options[objSelect.selectedIndex].text;
6 得到所选择项的值
objSelect.options[objSelect.selectedIndex].value;
--------------------------------动态表单、表单域个数不定的实现--------------------------------------------------------------
if (objSelect.selectedIndex > - 1 ){
// 说明选中
} else {
// 说明没有选中
}
2 删除被选中的项
objSelect.options[objSelect.selectedIndex] = null ;
3 增加项
objSelect.options[objSelect.length] = new Option( " 你好 " , " hello " );
4 修改所选择中的项
objSelect.options[objSelect.selectedIndex] = new Option( " 你好 " , " hello " );
5 得到所选择项的文本
objSelect.options[objSelect.selectedIndex].text;
6 得到所选择项的值
objSelect.options[objSelect.selectedIndex].value;
functioninsert(x)
{
varhtml="";
vari;
for(i=1;i < =x ;i++)
{
html =html +"<tr >< td > a"+i+" </ td >< td >< input name ="a" +i+""type ="text" id ="a" +i+"" /></ td ></ tr > ";
}
html=" < table border ="1" > "+html+" </ table > ";
document.getElementById("tablecontent").innerHTML=html;
}
functioncg(obj)
{
insert(obj.value);
}
在HTML中调用如下:
< form action ="" method ="post" name ="form1" id ="form1" >
< select name ="totalnum" id ="totalnum" onchange ="javascript:cg(this);" >
< option value ="10" > 10 </ option >
< option value ="2" > 2 </ option >
< option value ="50" > 50 </ option >
< option value ="100" > 100 </ option >
</ select >
< div id ="tablecontent" >
</ div >
</ form >
{
varhtml="";
vari;
for(i=1;i < =x ;i++)
{
html =html +"<tr >< td > a"+i+" </ td >< td >< input name ="a" +i+""type ="text" id ="a" +i+"" /></ td ></ tr > ";
}
html=" < table border ="1" > "+html+" </ table > ";
document.getElementById("tablecontent").innerHTML=html;
}
functioncg(obj)
{
insert(obj.value);
}
在HTML中调用如下:
< form action ="" method ="post" name ="form1" id ="form1" >
< select name ="totalnum" id ="totalnum" onchange ="javascript:cg(this);" >
< option value ="10" > 10 </ option >
< option value ="2" > 2 </ option >
< option value ="50" > 50 </ option >
< option value ="100" > 100 </ option >
</ select >
< div id ="tablecontent" >
</ div >
</ form >
---------------------------通过声明二维数组绑定SELECT下拉菜单 ----------------------------------------
<
HTML
>
< HEAD >
< TITLE > 动态改变下拉菜单内容示例 </ TITLE >
</ HEAD >
< SCRIPTLANGUAGE = javascript >
// 定义一个二维数组aArray,用于存放城市名称。
var aCity = new Array();
aCity[ 0 ] = new Array();
aCity[ 1 ] = new Array();
aCity[ 2 ] = new Array();
aCity[ 3 ] = new Array();
// 赋值,每个省份的城市存放于数组的一行。
aCity[ 0 ][ 0 ] = " --请选择-- " ;
aCity[ 1 ][ 0 ] = " --请选择-- " ;
aCity[ 1 ][ 1 ] = " 广州市 " ;
aCity[ 1 ][ 2 ] = " 深圳市 " ;
aCity[ 1 ][ 3 ] = " 珠海市 " ;
aCity[ 1 ][ 4 ] = " 汕头市 " ;
aCity[ 1 ][ 5 ] = " 佛山市 " ;
aCity[ 2 ][ 0 ] = " --请选择-- " ;
aCity[ 2 ][ 1 ] = " 长沙市 " ;
aCity[ 2 ][ 2 ] = " 株州市 " ;
aCity[ 2 ][ 3 ] = " 湘潭市 " ;
aCity[ 3 ][ 0 ] = " --请选择-- " ;
aCity[ 3 ][ 1 ] = " 杭州市 " ;
aCity[ 3 ][ 2 ] = " 苏州市 " ;
aCity[ 3 ][ 3 ] = " 温州市 " ;
function ChangeCity()
{
var i,iProvinceIndex;
iProvinceIndex = document.frm.optProvince.selectedIndex;
iCityCount = 0 ;
while (aCity[iProvinceIndex][iCityCount] != null )
iCityCount ++ ;
// 计算选定省份的城市个数
document.frm.optCity.length = iCityCount; // 改变下拉菜单的选项数
for (i = 0 ;i <= iCityCount - 1 ;i ++ ) // 改变下拉菜单的内容
document.frm.optCity[i] = new Option(aCity[iProvinceIndex][i]);
document.frm.optCity.focus();
}
</ SCRIPT >
< BODYONfocus = ChangeCity() >
< H3 > 选择你所在的省份及城市 </ H3 >
< FORMNAME = " frm " >
< P > 省份:
< SELECTNAME = " optProvince " SIZE = " 1 " ONCHANGE = ChangeCity() >
< OPTION >-- 请选择 --</ OPTION >
< OPTION > 广东省 </ OPTION >
< OPTION > 湖南省 </ OPTION >
< OPTION > 浙江省 </ OPTION >
</ SELECT >
</ P >
< P > 城市:
< SELECTNAME = " optCity " SIZE = " 1 " >
< OPTION >-- 请选择 --</ OPTION >
</ SELECT >
</ P >
</ FORM >
</ BODY >
</ HTML >
< HEAD >
< TITLE > 动态改变下拉菜单内容示例 </ TITLE >
</ HEAD >
< SCRIPTLANGUAGE = javascript >
// 定义一个二维数组aArray,用于存放城市名称。
var aCity = new Array();
aCity[ 0 ] = new Array();
aCity[ 1 ] = new Array();
aCity[ 2 ] = new Array();
aCity[ 3 ] = new Array();
// 赋值,每个省份的城市存放于数组的一行。
aCity[ 0 ][ 0 ] = " --请选择-- " ;
aCity[ 1 ][ 0 ] = " --请选择-- " ;
aCity[ 1 ][ 1 ] = " 广州市 " ;
aCity[ 1 ][ 2 ] = " 深圳市 " ;
aCity[ 1 ][ 3 ] = " 珠海市 " ;
aCity[ 1 ][ 4 ] = " 汕头市 " ;
aCity[ 1 ][ 5 ] = " 佛山市 " ;
aCity[ 2 ][ 0 ] = " --请选择-- " ;
aCity[ 2 ][ 1 ] = " 长沙市 " ;
aCity[ 2 ][ 2 ] = " 株州市 " ;
aCity[ 2 ][ 3 ] = " 湘潭市 " ;
aCity[ 3 ][ 0 ] = " --请选择-- " ;
aCity[ 3 ][ 1 ] = " 杭州市 " ;
aCity[ 3 ][ 2 ] = " 苏州市 " ;
aCity[ 3 ][ 3 ] = " 温州市 " ;
function ChangeCity()
{
var i,iProvinceIndex;
iProvinceIndex = document.frm.optProvince.selectedIndex;
iCityCount = 0 ;
while (aCity[iProvinceIndex][iCityCount] != null )
iCityCount ++ ;
// 计算选定省份的城市个数
document.frm.optCity.length = iCityCount; // 改变下拉菜单的选项数
for (i = 0 ;i <= iCityCount - 1 ;i ++ ) // 改变下拉菜单的内容
document.frm.optCity[i] = new Option(aCity[iProvinceIndex][i]);
document.frm.optCity.focus();
}
</ SCRIPT >
< BODYONfocus = ChangeCity() >
< H3 > 选择你所在的省份及城市 </ H3 >
< FORMNAME = " frm " >
< P > 省份:
< SELECTNAME = " optProvince " SIZE = " 1 " ONCHANGE = ChangeCity() >
< OPTION >-- 请选择 --</ OPTION >
< OPTION > 广东省 </ OPTION >
< OPTION > 湖南省 </ OPTION >
< OPTION > 浙江省 </ OPTION >
</ SELECT >
</ P >
< P > 城市:
< SELECTNAME = " optCity " SIZE = " 1 " >
< OPTION >-- 请选择 --</ OPTION >
</ SELECT >
</ P >
</ FORM >
</ BODY >
</ HTML >