• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
夜真寒
博客园    首页    新随笔    联系   管理    订阅  订阅

通过JavaScript操作HTML中select标签(转)

添加:

Js代码
1.function selectChange()   
2.{   
3. var sel=document.getElementById("select1");   
4. Option option = new Option("Text","Value");   
5. sel.add(option);   
6.}   
function selectChange()
{
  var sel=document.getElementById("select1");
  Option option = new Option("Text","Value");
  sel.add(option);
}  

删除所有:

Js代码
1.document.getElementById("select1").options.length=0;   
  document.getElementById("select1").options.length=0;删除单个Option元素:

Js代码
1.var sel=document.getElementById("select1");   
2.for(i=0;i<sel.options.length;i++)   
3.{   
4. if(sel.options[i].selected)   
5. {   
6. sel.options[i]=null; //设置为null就删除了这个元素   
7. }   
8.}   
var sel=document.getElementById("select1");
for(i=0;i<sel.options.length;i++)   
{   
  if(sel.options[i].selected)   
  {   
  sel.options[i]=null; //设置为null就删除了这个元素
  }   
} 取值:

Js代码
1.var sel=document.getElementById("select1"):   
2.var val=sel.options[sel.selectedIndex].value   //亦可以这样写  var val = sel.value;
3.alert(val); //得到Option的value   
4.var txt=sel.options[sel.selectedIndex].text   
5.alert(txt); //得到Option的文本(即Text)  

posted @ 2011-10-29 10:15  夜真寒  阅读(600)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3