<html>
<head>
<title>测试页面</title> 
<script type="text/javascript">
function test(){
var plist="1-小明 ¦2-小强 ¦3-小娟 ¦4-小狗"; 
var s = new Array();
s=plist.split(" ¦"); 
for(i=0;i<s.length;i++){ 
var zaire = new Option(s[i],  // The text property 
                      s[i],  // The value property 
                      false,    // The defaultSelected property 
                      false);   // The selected property  
// Display it in a Select element by appending it to the options array: 
var countries = document.all.man;  // Get the Select object 
countries.options[i] = zaire;
}

</script>
</head> 
<body>
<div align="center">
<select id="man" name="man" style="width: 130px;">
<option>--请选择--</option>
</select> 
<input type="button" name="but" value="添加下拉列表" onclick="test()">
</div>
</body>
</html>

解决方案 »

  1.   

    谢谢大哥,我想显示的是下拉列表的value是数字.text为姓名.
      

  2.   

    function test(){
    var plist="1-小明 ¦2-小强 ¦3-小娟 ¦4-小狗"; 
    var s = new Array();
    s=plist.split(" ¦"); 
    for(i=0;i<s.length;i++){ 
    var zaire = new Option(s[i].substr(2),  // The text property 
                           s[i].substr(0,1),  // The value property 
                           false,    // The defaultSelected property 
                           false);   // The selected property  
    // Display it in a Select element by appending it to the options array: 
    var countries = document.all.man;  // Get the Select object 
    countries.options[i] = zaire;
    }
    }
      

  3.   


    var flag=false;
    function addOptions()
    {
       if(!flag)
       {
          var plist="1-小明 ¦2-小强 ¦3-小娟 ¦4-小狗";
          var arrList=plist.split("|");
          for(var i in arrList)
          {
             var newItem=new Option(arrList[i],arrList[i]);
             $("nameList").options[$("nameList").length]=newItem;
          }
          flag=true;
       }
    }
    function $(objID)
    {
       return document.getElementById(objID);
    }
    <select id="nameList">
    <option>please select</option>
    </select>
    <input type="button" value="add options" onclick="addOptions()">================================================================
    基本是這樣.
      

  4.   

    針對3樓,只需稍微調整一下代碼即可:
    var newItem=new Option(arrList[i].substring(2),arrList[i].substring(0,1));
    =========================================================================
    可以試試.
      

  5.   

    人名字符串设计有问题,建议把名字的拼串改为如下:
    function test(){ 
    var plist="name1¦name2 ¦name3 ¦name4";  
    var s=plist.split(" ¦");  
    for(i=0;i <s.length;i++){  
    var zaire = new Option(s[i].substr(2) //显示名字,i //人名的序号);    
    var countries = document.all.man;  
    countries.options[i] = zaire; 

    }加分吧!