Jquery create select tag with data from array

前端 未结 2 1233
醉酒成梦
醉酒成梦 2021-01-12 09:23

I need to access database and update options of a select tag. My code is here.

 $(window).load(function () {

     $.getJSON(\'http://localhost/ABC/web/app_d         


        
相关标签:
2条回答
  • 2021-01-12 10:10

    Use the following code to append your tag:

    $("#idcatFill").append(function() {
        var elem = $('<select id ="idCat"  style="width:200px;margin-left:30px;margin-top:5px;">');
        for (var i = 0; i < length; i++) {
             row = data[i];
             elem.append('<option value="' + row['id'] + '">' + row['category_name'] + '</option>');
        }
        return elem;
    });
    

    you can't attach incomplete elements to the dom. You need to build your <select> tag before inserting it.

    0 讨论(0)
  • 2021-01-12 10:17

    Simply:

    $.each(data, function (index, value) {
      $('#idcatFill').append($('<option/>', { 
          value: index,
          text : value 
      }));
    });
    
    0 讨论(0)
提交回复
热议问题