Jquery getJSON 填充选择菜单问题

发布于 2024-08-06 12:27:31 字数 2405 浏览 4 评论 0原文

我正在使用 getJSON 填充选择菜单。我想知道是否有一种方法可以使用 jQuery 的 .each 函数来引入这些值?

当然必须有一种更简单的方法来完成这个......也许?

PHP 文件:

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

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

    echo json_encode($data);
?>

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>");
});

我的 json 输出如下所示:

[{
  "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"
}]

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"
}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

椒妓 2024-08-13 12:27:31
$.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);
});

如果您关心应用程序性能,则将 HTML 代码存储在变量中并在最后仅附加一次非常重要。

$.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.

风透绣罗衣 2024-08-13 12:27:31

这应该有效:

   $.getJSON('selectMenus.php', function(data){
        $.each(data, function(index,item) {
           $("select.month").append("<option value=" + item.monthID + ">" + item.month + "</option>"); 
    });
    });

This should work:

   $.getJSON('selectMenus.php', function(data){
        $.each(data, function(index,item) {
           $("select.month").append("<option value=" + item.monthID + ">" + item.month + "</option>"); 
    });
    });
酷炫老祖宗 2024-08-13 12:27:31

从伟大的书 jQuery in Action 中,这里有一种方法可以做你想做的事情想要编写自定义 jQuery 命令:

(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);    

然后:

$.getJSON('selectMenus.php', 
   function(data){
      $("select.month").loadSelect(data);
   }
);

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);
   }
);
指尖上得阳光 2024-08-13 12:27:31

使用@RaYell的方法....这对我有用:

$.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);
});

感谢大家对此的帮助!

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!!

落在眉间の轻吻 2024-08-13 12:27:31

您可以使用 for 循环。

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

如果你想获得最大的性能,你应该将 DOM 操作减少到最少,如下所示:

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(''));

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(''));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文