您的位置:首页 > Web前端 > JavaScript

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

2011-09-20 00:02 477 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: