Jquery getJSON 填充选择菜单问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您关心应用程序性能,则将 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.
这应该有效:
This should work:
从伟大的书 jQuery in Action 中,这里有一种方法可以做你想做的事情想要编写自定义 jQuery 命令:
然后:
From the great book jQuery in Action,here is a way to do what you want writing a custom jQuery command:
And then:
使用@RaYell的方法....这对我有用:
感谢大家对此的帮助!
Using @RaYell's method....this is what worked for me:
Thanks to everyone for your help on this!!
您可以使用
for
循环。如果你想获得最大的性能,你应该将 DOM 操作减少到最少,如下所示:
You can use a
for
loop.If you want to achieve maximum performance, you should reduce the DOM operations to a minimum, like this: