How can I use JSON data to populate the options of a select box?

后端 未结 6 2241
情歌与酒
情歌与酒 2020-11-28 04:25

I need to feed cities based on country of selection. I did it programmically but have no idea how to put JSON data into the select box. I tried several ways using jQuery, bu

6条回答
  •  情话喂你
    2020-11-28 05:28

    Why not just make the server return the names?

    ["Woodland Hills", "none", "Los Angeles", "Laguna Hills"]
    

    Then create the elements using JavaScript.

    $.ajax({
        url:'suggest.html',
        type:'POST',
        data: 'q=' + str,
        dataType: 'json',
        success: function( json ) {
            $.each(json, function(i, value) {
                $('#myselect').append($('

提交回复
热议问题