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

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

问题描述

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


$ b HTML代码

 < select id =searchDepartments> 
< / select> < input type =buttonvalue =SearchonClick =search(); />

JavaScript函数

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

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

解决方案

这个过程是创建一个 / code>节点,并将它添加为选择元素的子元素。



在普通的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);
}


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

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?

解决方案

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

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);
}

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

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