<select id="selectobj"></select>
1.取select的对象方法
①中情况:
如果有from的情况下可以这样取到 select 的对象
var objSelect = document.forms[0].elements["selectobj"];
score代表selectobj的id的名称
②中情况:
就是最常用的用id的方式取得
var objSelect = document.getElementById("selectobj");
2. js动态创建 option 有两种情况
①中情况:
var objSelect = document.getElementById("selectobj");
for(i=0;i<5;i++)
{
var varItem = new Option("zuo"+i, i);
objSelect.options.add(varItem);
}
②中情况:
for(j=0;j<5;j++)
{
var option_obj = document.createElement("OPTION");
option_obj.value = j;
option_obj.text = "shaobiao"+j;
objSelect.add(option_obj);
}
3. js动态显示其中的 option的值
①中情况:
objSelect.options[i].selected=true;
i 代表显示值的selectedIndex当你初始化的时候你想叫那个option的值显示
②中情况:
objSelect.options[i].selected=“select”;
4.js 动态移除 option的相应的值
下边的这两种情况都是可以的 你自己喜欢那个你自己选吧
①中情况:
核心语句:objSelect.options.remove(i);
i 代表显示值的selectedIndex当你初始化的时候你想叫那个option的值显示
function removeOptions(){
var opts = document.getElementById("selectobj");
for(var i=opts.length-1;i>=0;i--){
if(opts.options[i].selected==true){
opts.options.remove(i);
}
}
}
②中情况:
核心语句: opts.options[i]=null;
function removeOptions(){
var opts = document.getElementById("selectobj");
for(var i=opts.length-1;i>=0;i--){
if(opts.options[i].selected==true){
opts.options[i]=null;
}
}
}
5. 还有就是多选select时候。在这种情况先一般是在做人员权限的时候用的。
<select size="6" id="selectobj" multiple="multiple" > </select>
红色的就是select多选的一个属性
6. 如果你想全部清空select所有的值 你可以直接用
objSelect.options.length = 0;
7.设置select中value="paraValue"的Item为选中
document.all.selectobj.value = objItemValue;
8.得到select的当前选中项的value
var SValue = document.all.selectobj.value;
9.得到select的当前选中项的text
var SText = document.all.selectobj.options
[document.all.selectobj.selectedIndex].text;
10.得到select的当前选中项的Index
var SIndex= document.all.selectobj.selectedIndex;
呵呵这些就足够你去操作select标签了吧
如果喜欢讨论技术的可以加我 QQ :307495178