0

This is for a multi select box, so the user needs to select multiple items in one box.The for loop in the ajax will be different then in a drop down list. i Have JSON data which contains name, address etc. I also have in html a multi select box:

<select name="AccountDetails" id="Accounts" 
multiple>
  <option value="Name">Name</option>

</select>

I want to populate this multi select box from the JSON data, but i am not sure how.

I tried this:

     $.ajax({
               url: baseUrl + "accounts",
               method: "GET",
               dataType: "json",
               contentType: "application/json; charset=utf-8",
               accept: "application/json",
                success: function (returnData) {
                var data = returnData.value;

               for (var i = 0; i < data.length; i++) {
               AccountDetails += "<option value='" + data[i].accountid + "'>" + data[i].name "</option>";
     }
            $(document).ready(function () {
            var AccountDetails = '<option selected="selected" value="0">- Select -</option>';

   $("#Accounts").html(AccountDetails);

                $(".nav > ul").show();
            },
            error: function (msg) {
                console.log(msg);
            }
        });
    }
Sam
  • 149
  • 2
  • 15

1 Answers1

0

Can you try this `var data = [ { 'name': 'A', 'age': '23' }, { 'name': 'B', 'age': '23' } ];

for (var i = 0; i < data.length; i++) { $("#Accounts").append($(" ").attr("selected", "selected").text("name" + data[i].name + "age" + data[i].age));`

Pavan Kumar
  • 99
  • 4
  • 14