Jquery getJSON populate select menu question

前端 未结 5 1534
鱼传尺愫
鱼传尺愫 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条回答
  •  猫巷女王i
    2020-11-29 04:13

    You can use a for loop.

    for (var i = 0, len = data.length; i < len; i++)
        $("select.month")
             .append("");
    

    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] = "";
    }
    
    $("select.month").append(html.join(''));
    

提交回复
热议问题