Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

javascript - Jquery getJSON populate select menu question

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 an easier way to accomplish this...maybe?

PHP file:

<?php
    $queryMonth = "SELECT monthID, month FROM months";
    $result = $db->query($queryMonth);

      while($rowMonth = $db->fetch_assoc($result)) :
        $data[] = $rowMonth;
      endwhile;

    echo json_encode($data);
?>

The jQuery:

$.getJSON('selectMenus.php', function(data) {
  $("select.month").append("<option value=" + data[0].monthID + ">" + data[0].month + "</option>");
  $("select.month").append("<option value=" + data[1].monthID + ">" + data[1].month + "</option>");
  $("select.month").append("<option value=" + data[2].monthID + ">" + data[2].month + "</option>");
  $("select.month").append("<option value=" + data[3].monthID + ">" + data[3].month + "</option>");
  $("select.month").append("<option value=" + data[4].monthID + ">" + data[4].month + "</option>");
  $("select.month").append("<option value=" + data[5].monthID + ">" + data[5].month + "</option>");
  $("select.month").append("<option value=" + data[6].monthID + ">" + data[6].month + "</option>");
  $("select.month").append("<option value=" + data[7].monthID + ">" + data[7].month + "</option>");
  $("select.month").append("<option value=" + data[8].monthID + ">" + data[8].month + "</option>");
  $("select.month").append("<option value=" + data[9].monthID + ">" + data[9].month + "</option>");
  $("select.month").append("<option value=" + data[10].monthID + ">" + data[10].month + "</option>");
  $("select.month").append("<option value=" + data[11].monthID + ">" + data[11].month + "</option>");
});

My json output looks like this:

[{
  "monthID": "1",
  "month": "January"
}, {
  "monthID": "2",
  "month": "February"
}, {
  "monthID": "3",
  "month": "March"
}, {
  "monthID": "4",
  "month": "April"
}, {
  "monthID": "5",
  "month": "May"
}, {
  "monthID": "6",
  "month": "June"
}, {
  "monthID": "7",
  "month": "July"
}, {
  "monthID": "8",
  "month": "August"
}, {
  "monthID": "9",
  "month": "Septemeber"
}, {
  "monthID": "10",
  "month": "October"
}, {
  "monthID": "11",
  "month": "November"
}, {
  "monthID": "12",
  "month": "December"
}]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...