如何添加选项以在 jQuery 中选择列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1730360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add option to select list in jQuery
提问by danila dinan
My select list is called dropListBuilding
. The following code does not seem to work:
我的选择列表称为dropListBuilding
. 以下代码似乎不起作用:
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);
}
This line dies:
这条线死了:
$("#dropListBuilding").addOption(val, text, false);
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.
在 jQuery 的下拉列表中添加项目的权利是什么?我在没有那条线的情况下进行了测试,并且建筑物变量确实有我的数据元素。
回答by HaMinh Nguyen
Don't make your code so complicated. It can be done simply as below by using a foreach-like iterator:
不要让你的代码如此复杂。它可以通过使用类似 foreach 的迭代器来简单地完成,如下所示:
$.each(buildings, function (index, value) {
$('#dropListBuilding').append($('<option/>', {
value: value,
text : value
}));
});
回答by James
Doing it this way has always worked for me, I hope this helps.
这样做一直对我有用,我希望这会有所帮助。
var ddl = $("#dropListBuilding");
for (k = 0; k < buildings.length; k++)
ddl.append("<option value='" + buildings[k]+ "'>" + buildings[k] + "</option>");
回答by Soufiane Hassou
$('#dropListBuilding').append('<option>'+val+'</option>');
回答by duckyflip
Your code fails because you are executing a method (addOption) on the jQuery object (and this object does not support the method)
您的代码失败,因为您正在 jQuery 对象上执行方法 (addOption)(并且此对象不支持该方法)
You can use the standard Javascript function like this:
您可以像这样使用标准的 Javascript 函数:
$("#dropListBuilding")[0].options.add( new Option("My Text","My Value") )
回答by Parag Gajjar
This is working fine, try out this.
这工作正常,试试这个。
var ob = $("#myListBox");
for (var i = 0; i < buildings.length; i++) {
var val = buildings[i];
var text = buildings[i];
ob.prepend("<option value="+ val +">" + text + "</option>");
}
回答by Bob
It looks like you want this pluging as it follows your existing code, maybe the plug in js file got left out somewhere.
看起来你想要这个插件,因为它遵循你现有的代码,也许插件 js 文件被遗漏在某个地方。
http://www.texotela.co.uk/code/jquery/select/
http://www.texotela.co.uk/code/jquery/select/
var myOptions = {
"Value 1" : "Text 1",
"Value 2" : "Text 2",
"Value 3" : "Text 3"
}
$("#myselect2").addOption(myOptions, false);
// use true if you want to select the added options ? Run
回答by Sebien
If you do not want to rely on the 3.5 kB plugin for jQuery or do not want to construct the HTML string while escapping reserved HTML characters, here is a simple way that works:
如果您不想依赖 jQuery 的 3.5 kB 插件,或者不想在转义保留的 HTML 字符时构造 HTML 字符串,这里有一个简单的方法:
function addOptionToSelectBox(selectBox, optionId, optionText, selectIt)
{
var option = document.createElement("option");
option.value = optionId;
option.text = optionText;
selectBox.options[selectBox.options.length] = option;
if (selectIt) {
option.selected = true;
}
}
var selectBox = $('#veryImportantSelectBox')[0];
addOptionToSelectBox(selectBox, "ID1", "Option 1", true);
回答by faisalbhagat
For me this one worked
对我来说,这个有效
success: function(data){
alert("SUCCCESS");
$.each(data,function(index,itemData){
console.log(JSON.stringify(itemData));
$("#fromDay").append( new Option(itemData.lookupLabel,itemData.id) )
});
}
回答by mayur vadher
$.each(data,function(index,itemData){
$('#dropListBuilding').append($("<option></option>")
.attr("value",key)
.text(value));
});