gpt4 book ai didi

javascript - 使用 jquery 将 json 字符串与下拉列表绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 12:32:20 26 4
gpt4 key购买 nike

我是 jQuery 新手。我正在使用 gson-2.2.3.jar 在 servlet 中创建 json 字符串:

Gson gs = new Gson();
String json = gs.toJson(names);
System.out.println(json);
PrintWriter out = resp.getWriter();
out.println(json);
out.close();

下面是我尝试将 json 字符串与下拉列表绑定(bind)的两个注释代码,但它们都不起作用:

$.ajax({
type:"POST",
url: "DropDownController",
success: function(result){
alert(result);
/* for (var id in result) {
$("#drpDown").append('<option value="'+id+'">'+id+'</option>');
} */
/* $.each(result, function (index, value) {
$("#drpDown").append('<option value="'+value.id+'">'+value.name+'</option>');
}); */
}
});

成功中出现的警报消息框显示以下格式的 json 字符串:

[“阿拉巴马州”、“加利福尼亚州”、“阿拉斯加州”、“俄亥俄州”]

请告诉我如何将上述 json 字符串 数据与下拉列表绑定(bind)。

最佳答案

尝试这样:

$.ajax({
type: 'POST',
url: 'DropDownController',
dataType: 'json',
success: function(result) {
$.each(result, function() {
$("#drpDown").append(
$('<option/>', {
value: this,
html: this
})
);
});
}
});

关于javascript - 使用 jquery 将 json 字符串与下拉列表绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com