Jquery getJSON populate select menu question

前端 未结 5 1532
鱼传尺愫
鱼传尺愫 2020-11-29 03:28

I am populating a select menu with getJSON. I am wondering if there\'s a way that I can use jQuery\'s .each function to bring in these values?

Surely there must be

相关标签:
5条回答
  • 2020-11-29 04:06

    Using @RaYell's method....this is what worked for me:

    $.getJSON('months.php', function(data){
        var html = '';
        var len = data.length;
        for (var i = 0; i < len; i++) {html += '<option value="' + data[i].monthId + '">' +    data[i].month + '</option>';
        }
        $('select.month').append(html);
    });
    

    Thanks to everyone for your help on this!!

    0 讨论(0)
  • 2020-11-29 04:08

    From the great book jQuery in Action,here is a way to do what you want writing a custom jQuery command:

    (function($) {
      $.fn.emptySelect = function() {
        return this.each(function(){
          if (this.tagName=='SELECT') this.options.length = 0;
        });
      }
    
      $.fn.loadSelect = function(optionsDataArray) {
        return this.emptySelect().each(function(){
          if (this.tagName=='SELECT') {
            var selectElement = this;
            $.each(optionsDataArray,function(index,optionData){
              var option = new Option(optionData.caption,
                                      optionData.value);
              if ($.browser.msie) {
                selectElement.add(option);
              }
              else {
                selectElement.add(option,null);
              }
            });
          }
        });
      }
    })(jQuery);    
    

    And then:

    $.getJSON('selectMenus.php', 
       function(data){
          $("select.month").loadSelect(data);
       }
    );
    
    0 讨论(0)
  • 2020-11-29 04:12
    $.getJSON('selectMenus.php', function(data){
        var html = '';
        var len = data.length;
        for (var i = 0; i< len; i++) {
            html += '<option value="' + data[i].monthId + '">' + data[i].month + '</option>';
        }
        $('select.month').append(html);
    });
    

    Storing the HTML code in a variable and appending it only once at the end is very important if you care about your app performance.

    0 讨论(0)
  • 2020-11-29 04:13

    You can use a for loop.

    for (var i = 0, len = data.length; i < len; i++)
        $("select.month")
             .append("<option value=" + data[i].monthID + ">" + data[i].month + "</option>");
    

    If you want to achieve maximum performance, you should reduce the DOM operations to a minimum, like this:

    var html = [];
    
    for (var i = 0, len = data.length; i < len; i++) {
        html[html.length] = "<option value=";
        html[html.length] = data[i].monthID;
        html[html.length] = ">";
        html[html.length] = data[i].month;
        html[html.length] = "</option>";
    }
    
    $("select.month").append(html.join(''));
    
    0 讨论(0)
  • 2020-11-29 04:17

    This should work:

       $.getJSON('selectMenus.php', function(data){
            $.each(data, function(index,item) {
               $("select.month").append("<option value=" + item.monthID + ">" + item.month + "</option>"); 
        });
        });
    
    0 讨论(0)
提交回复
热议问题