在下面所有代码前最好加上这句:
1
|
var selectId=document.getElemengById('selectId');
|
清空select的项
1
|
selectId.options.length = 0;
|
如果留下第一行的话就是
1
|
selectId.options.length = 1;
|
向select选项中 加入一个Option
1
2
3
|
var varOption = new Option(objOptionText,objOptionValue);
selectId.options[selectId.options.length] = varOption;
//或selectId.options.add(varOption);
|
从select选项中 删除一个Option
1
2
3
4
5
6
7
8
|
for(var i=0;i<selectId.options.length;i++)
{
if(selectId.options[i].value == objOptionValue)
{
selectId.options.remove(i);
break;
}
}
|
设置select中text=”paraText”的第一个Option为选中
1
2
3
4
5
6
7
8
9
|
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为选中
1
|
selectId.value = objOptionValue;
|
得到select的当前选中项的value
1
|
var currSelectValue = selectId.value;
|
得到select的当前选中项的text
1
|
var currSelectText = selectId.options[selectId.selectedIndex].text;
|
得到select的当前选中项的Index
1
|
var currSelectIndex = selectId.selectedIndex;
|