html 下拉列表操作

 
查看文章
 
html 下拉列表操作
2008-01-21 14:23

<form id="f">
          <select size="1" name="s">
                          <option value="1">第一项</option>
                          <option value="2">第二项</option>
          </select>
</form>
<script type="text/javascript" language="javascript">
<!--
var f = document.getElementById("f");
//列表项数目(有两种方法)
document.write(f.s.options.length);
document.write(f.s.length);
//当前选中项的下标(从 0 开始)(有两种方法)
//如果选择了多项,则返回第一个选中项的下标
document.write(f.s.options.selectedIndex);
document.write(f.s.selectedIndex);
//检测某一项是否被选中
document.write(f.s.options[0].selected);
//获得某一项的值和文字
document.write(f.s.options[0].value);
document.write(f.s.options[1].text);
//删除某一项
f.s.options[1] = null;
//追加一项
f.s.options[f.s.options.length] = new Option("追加的text", "追加的value");
//更改一项
f.s.options[1] = new Option("更改的text", "更改的value");
//也可以直接设置该项的 text 和 value
//-->
</script>

 

======================

 

<select name="a">
      <option value="">pls select</option>
      <option value="1">aaaa1</option>
      <option value="2">aaaa2</option>
      <option value="3">aaaa3</option>
      <option value="4">aaaa4</option>
</select>
<input type="button" onclick="remove()" value="remove">
<input type="button" onclick="add()" value="add">
<script language="javascript">
      function remove(){
          var obj = document.getElementById("a")
          while(obj.length>1)
          {
              obj.options.remove(obj.length-1)
          }
      }
    
      function add(){
          var obj = document.getElementById("a")        
          for(i=1;i<5;i++)
          {
              var option = document.createElement("option")
              obj.options.add(option)
              obj.options[i].text = "aaaa"+i;
              obj.options[i].value = i;
          }
      }
</script>

posted @ 2008-05-20 17:14  南守拥  阅读(2460)  评论(0编辑  收藏  举报