我是 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/