最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binding json string with drop-down using jquery - Stack Overflow

programmeradmin4浏览0评论

I am new to jQuery. I am creating the json string in my servlet using gson-2.2.3.jar:

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

Below are the two mented codes I tried for binding the json string with drop-down but none of them works:

$.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>');
            }); */
         }
      });

The alert message box present in the success displays the json string in below format:

["Alabama","California","Alaska","Ohio"]

Kindly let me know how to bind the above json string data with the drop-down.

I am new to jQuery. I am creating the json string in my servlet using gson-2.2.3.jar:

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

Below are the two mented codes I tried for binding the json string with drop-down but none of them works:

$.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>');
            }); */
         }
      });

The alert message box present in the success displays the json string in below format:

["Alabama","California","Alaska","Ohio"]

Kindly let me know how to bind the above json string data with the drop-down.

Share Improve this question asked Dec 28, 2013 at 9:43 user182944user182944 8,06736 gold badges112 silver badges182 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Try like this:

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

Try this

var jso=["Alabama","California","Alaska","Ohio"]
for (var i in jso) {
  $("#test").append('<option value="'+jso[i]+'">'+jso[i]+'</option>');
} 

DEMO

发布评论

评论列表(0)

  1. 暂无评论