[英]How to populate select list with jquery?
I am novice in HTML, javaScript or jQuery, I am basically an Oracle DBA and Developer.我是 HTML、javaScript 或 jQuery 的新手,我基本上是一名 Oracle DBA 和开发人员。 I need to know how to populate select list with jquery.我需要知道如何使用 jquery 填充选择列表。 I have created a string from database query which is as mentioned below.我从数据库查询中创建了一个字符串,如下所述。 Now I want this to be populated in a select list.现在我希望将其填充到选择列表中。 For example I have the string like:例如我有这样的字符串:
<select id="my-select" name="emplist">
<option value="7839">KING</option>
<option value="7782">CLARK</option>
<option value="7934">MILLER</option>
<option value="21">MAMUN</option></select>
and I like to populate the select list item called '#SELECT_LIST' with the data above.我喜欢用上面的数据填充名为“#SELECT_LIST”的选择列表项。 How can I do that?我怎样才能做到这一点? Now I want the selct item to be populated with the string.现在我想用字符串填充 selct 项目。
Please respond, your answer will help me out.请回答,你的回答会帮助我。
Thanks.谢谢。
Using Array.prototype.forEach() and let使用 Array.prototype.forEach() 并让
let data = [{"code":1, "name": "A"},
{"code":2, "name": "B"},
{"code":3, "name": "C"},
{"code":4, "name": "D"}];
data.forEach(function(e, i){
$('#select-id').append($('<option></option>').val(e.code).text(e.name));
});
You can use jquery to do it:您可以使用 jquery 来做到这一点:
$(document).ready(function(){
//init data
var arrayList = [
{"Id": 100, "Name": "Abc"},
{"Id": 200, "Name": "XYZ"}
];
for (var i = 0; i <= arrayList.length; i++) {
$('#SELECT_LIST').append('<option value="' + arrayList[i].Id + '">' + arrayList[i].Name + '</option>');
}
});
$("#my-select").val("7839");
Maybe you can look at this.也许你可以看看这个。 http://learn.jquery.com/using-jquery-core/ http://learn.jquery.com/using-jquery-core/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.