如何在JavaScript / jQuery中创建动态SELECT下拉列表? [英] How to create a dynamic SELECT drop-down list in JavaScript/jQuery?

查看:87
本文介绍了如何在JavaScript / jQuery中创建动态SELECT下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在组合框中显示Dept Table中的所有部门名称。
我有一个获取所有Dept名称的函数。
如何使用javaScript或jQuery在运行时动态创建组合框。

I want to display the all the department names in the Dept Table in a combo box. I have a function which fetches all the Dept name. How can I dynamically create combo box in runtime, using javaScript or jQuery.

HTML CODE

     <select id="searchDepartments">
     </select> <input type="button" value="Search" onClick="search();" />

JavaScript功能

function getDepartments(){
EmployeeManagement.getDeptList(function(deptList/*contains n-(dept.id, dept.name)*/{
    for(i = 0; i<deptList.length; i++){

我怎样才能写一个生成(添加)列表选项的代码?

How can I able to write a code that generates(adds) options to the list?

推荐答案

该过程是创建一个选项列表中每个项目的节点,并将其添加为 select 元素的子项。

The process is to create an option node for each item in the list, and add it as a child of the select element.

简单的javascript:

In plain javascript:

var sel = document.getElementById('searchDepartments');
var opt = null;

for(i = 0; i<deptList.length; i++) { 

    opt = document.createElement('option');
    opt.value = deptList[i].id;
    opt.innerHTML = deptList[i].name;
    sel.appendChild(opt);
}

这篇关于如何在JavaScript / jQuery中创建动态SELECT下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆