JS操作SELECT表单大全,赋默认值,取值,增,删等

转载地址:http://www.enjoyphp.com/2009/05/05/javascript/js-select/

经常用到的代码,却总是忘掉。总结一下,放在这里。
在下面所有代码前最好加上这句:

var selectId=document.getElemengById('selectId');

清空select的项

selectId.options.length = 0;

如果留下第一行的话就是

selectId.options.length = 1;

向select选项中 加入一个Option

var varOption = new Option(objOptionText,objOptionValue);

selectId.options[selectId.options.length] = varOption;

//或selectId.options.add(varOption);

从select选项中 删除一个Option

for(var i=0;i<selectId.options.length;i++)

{

if(selectId.options[i].value == objOptionValue)

{

selectId.options.remove(i);

break;

}

}

设置select中text=”paraText”的第一个Option为选中

for(var i=0;i<selectId.options.length;i++)

{

if(selectId.options[i].text == objOptionText)

{

selectId.options[i].selected = true;

isExit = true;

break;

}

}

设置select中value=”paraValue”的Option为选中

selectId.value = objOptionValue;

得到select的当前选中项的value

var currSelectValue = selectId.value;

得到select的当前选中项的text

var currSelectText = selectId.options[selectId.selectedIndex].text;

得到select的当前选中项的Index

var currSelectIndex = selectId.selectedIndex;

posted @ 2010-04-14 13:16  济阳补丁  阅读(6331)  评论(1编辑  收藏  举报