jQuery:填充下拉列表的最佳实践? [英] jQuery: Best practice to populate drop down?

查看:68
本文介绍了jQuery:填充下拉列表的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到的示例似乎是次优的,因为它涉及串联字符串,而jQuery则不是.通常看起来像这样:

The example I see posted all of the time seems like it's suboptimal, because it involves concatenating strings, which seems so not jQuery. It usually looks like this:

$.getJSON("/Admin/GetFolderList/", function(result) {
    for (var i = 0; i < result.length; i++) {
        options += '<option value="' + result[i].ImageFolderID + '">' + result[i].Name + '</option>';
    }
});

有更好的方法吗?

推荐答案

安德烈亚斯·格里奇(Andreas Grech)非常接近...实际上是this(请注意对this的引用,而不是循环中的项目):

Andreas Grech was pretty close... it's actually this (note the reference to this instead of the item in the loop):

var $dropdown = $("#dropdown");
$.each(result, function() {
    $dropdown.append($("<option />").val(this.ImageFolderID).text(this.Name));
});

这篇关于jQuery:填充下拉列表的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆