JavaScript关于select的相关操作说明
更新时间:2010年01月13日 03:43:44 作者:
JavaScript关于select的相关操作参考代码。
一、 插入option
1、DOM方法
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);
2、new Option方法
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));
二、 清空option
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //清空Select里面的options
三、 设置默认选中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默认选中第二项
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延迟是为了防止dom渲染问题
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比较推荐的用setAttribute来设置
1、DOM方法
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);
2、new Option方法
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));
二、 清空option
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //清空Select里面的options
三、 设置默认选中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默认选中第二项
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延迟是为了防止dom渲染问题
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比较推荐的用setAttribute来设置
相关文章
用 Javascript 验证表单(form)中的单选(radio)值
在用 Javascript 验证表单(form)中的单选框(radio)是否选中时,很多新手都会遇到问题,原因是 radio 和普通的文本框在获取值的时候有很大不同.2009-09-09在修改准备发的批量美化select+可修改select时,在非IE下发现了几个问题
在修改准备发的批量美化select+可修改select时,在非IE下发现了几个问题...2007-01-01
最新评论