javascript - 通过jQuery动态生成Drop Down列表的内容

标签 javascript jquery ajax json drop-down-menu

我是 Javascript、JSON 和 jQuery 的新手。所以请对我宽容一些。我有一个包含下拉列表的 JSP 页面。加载页面时会填充下拉列表的内容。我编写了一个Servlet,以Map的形式返回下拉列表的内容,并将其转换为JSON字符串并通过response.getWriter().write(json);发送回jsp但是,我无法从 jsp 端获取结果,并从结果填充下拉列表的内容。这是我的代码

customer.jsp

$(document).ready(function() {
  getCustomerOption('customer'); //try to pre-populate the customer drop down list
});

function getCustomerOption(ddId) {
  var dd = $('#' + ddId);
  $.getJSON("http://localhost:8080/WebApps/DDListJASON", function(opts) {
      $('>option', dd).remove(); // Remove all the previous option of the drop down
      if (opts) {
        $.each(opts, function(key, value) {
            dd.append($('<option/>').val(key).text(value));
          }
        }
      });
  }

向下生成下拉列表的位置

<select id="customer" name="customer">  
    <option></option>  
</select>

结果是没有任何内容被填充到列表中。好伤心

最佳答案

我认为您在文档就绪中调用了错误的函数

不应该吗

getInitialOption('customer');

而不是

getCustomerOption('customer');

关于javascript - 通过jQuery动态生成Drop Down列表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353881/

相关文章:

javascript - 如何初始化数组变量

javascript - AngularJS:自定义 $anchorScroll 提供程序执行

javascript - jquery window.resize 不工作

javascript - 如何使用 jQuery 单独引用动态生成的 Bootstrap 面板

javascript - ('afterChange' 上的光滑 slider )

javascript - Kendo 网格详细信息模板无法使用 ajax 表单发布 (MVC) 再次(第二次)加载网格

php - WooCommerce - 在 Ajax 更新后计算购物车总数

javascript - 为什么这个 Facebook javascript-sdk 登录代码会一次又一次自动登录用户?

javascript - Jquery Time-Picker 隐藏在表内

javascript - 如何动态地将 jquery ui 事件处理程序添加到动态创建的按钮?