javascript - 如何使用 javascript 填充下拉列表

标签 javascript

如何在java脚本中填充下拉值?

<script type="text/javascript">

var creatLimit = 5;
var fCount=0;   
function addFileElement() 
{

    if(fCount <creatLimit )
    {   
     /*var option = document.createElement("option");
        option.value = '0'; option.innerHTML = ' -- '; select.appendChild(option);
        option.value = '1'; option.innerHTML = 'item 1'; select.appendChild(option);
        option.value = '2'; option.innerHTML = 'item 2'; select.appendChild(option);*/
    var fObject = document.getElementById("agencySection"); 
    //var addButton = document.createElement(label);

    var addButton = document.createElement("select");
    var agency = document.getElementById("agencyLabelSection");
    var addButton2 = document.createElement("option");

    //<label for="firstname">First name:</label> 
    //<label for="agencyLabelSection">agency:</label> 
    //<input type="text" name="agencyLabelSection" id="agencyLabelSection" />

    addButton.type= "select";   
    addButton2.type= "option";
    //for (var fObject in addButton) {
    addButton.name="userRoleBeanList["+fCount+"]";  
    addButton.setAttribute("class", "normal");
    addButton.style.width= "250px"; 
    addButton.onkeydown = function(){
        blur();

    };  //}
    //document.write("<p> Agency:</p>");
    addButton2.name="userRoleBeanList["+fCount+"]"; 
    addButton2.setAttribute("class", "normal");
    addButton2.onkeydown = function(){
        blur();
    };  
    var o2 = document.createElement("br");
    var o3 = document.createElement("br");

    fObject.appendChild(addButton);
    fObject.appendChild(o2);
    fObject.appendChild(o3);
    agency.appendChild(addButton2);

    var fObject1 = document.getElementById("roleSection");  
    var addButton1 = document.createElement("select");

    var role = document.getElementById("roleLabelSection");
    var addButton3 = document.createElement("option");

    addButton1.type= "select";  
    addButton3.type= "option";

    addButton1.name="userRoleBeanList["+fCount+"]"; 
    addButton1.setAttribute("class", "normal");
    addButton1.style.width= "250px";    
    addButton1.onkeydown = function(){
        blur();
    };  

    var o4 = document.createElement("br");
    var o5 = document.createElement("br");

    fObject1.appendChild(addButton1);
    fObject1.appendChild(o4);
    fObject1.appendChild(o5);
    role.appendChild(addButton3);

    fCount++;
    }   
}
</script>

最佳答案

提出了同样的问题here答案就是

ddl.options[i] = theOption;

此代码示例展示了如何向下拉列表添加变量:

var ddl = document.getElementById( 'myDropdown' );
 var theOption = new Option;
 var x;
 var i;

 for(i = 0; i < 999; i++) {
 x = i + 1;
 theOption.text = x;
 theOption.value = x;
 ddl.options[i] = theOption;
 }

如果您要编辑您的问题,以便我们知道下拉列表名称和所需值是什么,那么我可以为您提供更多帮助

关于javascript - 如何使用 javascript 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919011/

相关文章:

javascript - 动态创建函数并将其传递到 Google 文档产品(文档、表格等)的自定义菜单中

javascript - Windows Mobile 上的媒体查询检测

Javascript Array Scope - 这里是新手

javascript - Bootstrap 模式不起作用?

javascript - Handlebars Helper 比较值(如果 v1 === v2),并渲染上层范围?

javascript - 如何发送预先构建的请求

javascript - Node Express 和 csurf - 403(禁止)无效的 csrf token

javascript - 我如何在响应式 HTML5 图像中使用 srcset 和大小来获取占其容器百分比的图像?

javascript - (true + false)> 2 + true;为什么返回false?

javascript - jQuery DOMWindow 脚本不释放内存