如何添加选项的jQuery选择列表 [英] How to add option to select list in jQuery

查看:135
本文介绍了如何添加选项的jQuery选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的选择列表被称为 dropListBuilding 。下面code似乎没有工作:

My select list is called dropListBuilding. The following code does not seem to work:

 for (var i = 0; i < buildings.length; i++) {
     var val = buildings[i];
     var text = buildings[i];
     alert("value of builing at: " + i.toString() + " is: " + val);
     $("#dropListBuilding").addOption(val, text, false);
 }

这行模:

$("#dropListBuilding").addOption(val, text, false);

什么是项目jQuery中添加下拉的权利?我没有这条线已经测试和建筑物变量确实有我的数据元素。

What is the right to add items to the drop down in jQuery? I have tested without that line and the buildings variable does have my data element.

推荐答案

不要让你的code如此复杂。它可以简单地如下文通过使用foreach状迭代进行:

Don't make your code so complicated. It can be done simply as below by using a foreach-like iterator:

$.each(buildings, function (index, value) {
    $('#dropListBuilding').append($('<option/>', { 
        value: value,
        text : value 
    }));
});      

这篇关于如何添加选项的jQuery选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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