Question

I just want to fill combobox by JSON. (using jquery mobile) for example: this is my string (JSON):

var response = [{
      "A":"a2",
      "B":"b2",
      "C":"c2"
     },
     {
      "A":"a3",
      "B":"b3",
      "C":"c3"
    },
    {
      "A":"a4",
      "B":"b4",
      "C":"c4"
    }];

and this is my code:

...

<div>
<h1>Choose:</h1>
    <select name="myDropDownA" id="myDropDownA">
        <option>myOption</option>  
    </select>
    <select name="myDropDownB" id="myDropDownB">
        <option>myOption</option>  
    </select>
    <script>
     $(response.A).each(function()
             {
                 var option = $('<option />');
                 option.attr('value', this.value).text(this.label);
                 $('#myDropDownA').append(option);
             });
    </script>
    <INPUT type="button" value="Mybutton" onclick="Mybutton" />
    </div>

I want myDropDownA to be the selecting option A, myDropDownB to be the selecting option B. and it's not working. why?

Was it helpful?

Solution

Your $each is wrong. Check reference

var myDropDownA = $("#myDropDownA");
var myDropDownB = $("#myDropDownB");
$.each(response, function(index,obj)
{
     //alert(index + ": " + obj.A);
    myDropDownA.append($("<option />").val(obj.A).text(obj.A));
    myDropDownB.append($("<option />").val(obj.B).text(obj.B));
});

Working demo: JSfiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top