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

后端 未结 6 2201
情歌与酒
情歌与酒 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:14

    Take a look at JQuery view engine and just load the array into a dropdown:

    $.ajax({
        url:'suggest.html',
        type:'POST',
        data: 'q=' + str,
        dataType: 'json',
        success: function( json ) {
              // Assumption is that API returned something like:["North","West","South","East"];
              $('#myselect').view(json);
        }
    });
    

    See details here: https://jocapc.github.io/jquery-view-engine/docs/ajax-dropdown

提交回复
热议问题