javascript 中关于select 的应用和相关操作

系统 1420 0
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;
--------------------------------动态表单、表单域个数不定的实现--------------------------------------------------------------
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 >

---------------------------通过声明二维数组绑定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 >

javascript 中关于select 的应用和相关操作


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论