简体   繁体   English

Javascript遍历对象以填充HTML选择

[英]Javascript looping through an object to populate an HTML select

I have this java script code: 我有这个Java脚本代码:

var LANGS = {
    "C#": [10, "text/x-csharp"],
    "C/C++": [7, "text/x-c++src"],
    "Clojure": [2, "text/x-clojure"],
    "Java": [8, "text/x-java"],
    "Go": [6, "text/x-go"],
    "Plain JavaScript": [4, "text/javascript"],
    "PHP": [3, "text/x-php"],
    "Python": [0, "text/x-python"],
    "Ruby": [1, "text/x-ruby"],
    "Scala": [5, "text/x-scala"],
    "VB.NET": [9, "text/x-vb"],
    "Bash": [11, "text/x-bash"],
    "Objective-C": [12,"text/x-objectivec"],
    "MySQL": [13,"text/x-sql"],
    "Perl": [14, "text/x-perl"],
}

I right now I have the following code to show that information as an alert: 我现在有以下代码将这些信息显示为警报:

$('#langhelp').on('click', function () {
            var msg = "These are the languages and their langids: \n[LANGID]: [LANGUAGE]\n";
            var langs = [];
            for (var i in LANGS) {
                msg += LANGS[i][0] + ": " + i + "\n";
            }
            alert(msg);
        });

But what I would like to do is populate an HTML select with this data only I can not figure out how to do so, I have looked at this question but don't see how to append the options to the select element. 但是我想做的是用这个数据填充HTML select,但我不知道该怎么做,我已经看了这个问题,但是看不到如何将选项附加到select元素。

Just loop over the list and create options, then append them to select element. 只需遍历列表并创建选项,然后将其附加到选择元素即可。 Finally, append select into desired container. 最后,将select附加到所需的容器中。

In my example, I use $.fn.map to iterate over the list of items and create array of option elements. 在我的示例中,我使用$.fn.map遍历项目列表并创建option元素数组。 This array (actually it's jQuery array-like object) is then appended as html content to the newly created select element. 然后将此数组(实际上是类似于jQuery数组的对象)作为html内容附加到新创建的select元素。

 var LANGS = { "C#": [10, "text/x-csharp"], "C/C++": [7, "text/x-c++src"], "Clojure": [2, "text/x-clojure"], "Java": [8, "text/x-java"], "Go": [6, "text/x-go"], "Plain JavaScript": [4, "text/javascript"], "PHP": [3, "text/x-php"], "Python": [0, "text/x-python"], "Ruby": [1, "text/x-ruby"], "Scala": [5, "text/x-scala"], "VB.NET": [9, "text/x-vb"], "Bash": [11, "text/x-bash"], "Objective-C": [12,"text/x-objectivec"], "MySQL": [13,"text/x-sql"], "Perl": [14, "text/x-perl"], }; var $select = $('<select>', { html: $.map(LANGS, function(value, key) { return '<option value="' + value[0] + '">' + key + '</option>'; }) }); $select.appendTo('body'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Here is your: 这是你的:

  var LANGS = { "C#": [10, "text/x-csharp"], "C/C++": [7, "text/x-c++src"], "Clojure": [2, "text/x-clojure"], "Java": [8, "text/x-java"], "Go": [6, "text/x-go"], "Plain JavaScript": [4, "text/javascript"], "PHP": [3, "text/x-php"], "Python": [0, "text/x-python"], "Ruby": [1, "text/x-ruby"], "Scala": [5, "text/x-scala"], "VB.NET": [9, "text/x-vb"], "Bash": [11, "text/x-bash"], "Objective-C": [12, "text/x-objectivec"], "MySQL": [13, "text/x-sql"], "Perl": [14, "text/x-perl"], } $.each(LANGS, function(key, keyValue) { var option = $('<option />').prop('value', keyValue[0]).text(key); $('select').append(option); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select></select> 

you can also try something like this (plain js): 您还可以尝试这样的操作(普通js):

 var LANGS = { "C#": [10, "text/x-csharp"], "C/C++": [7, "text/x-c++src"], "Clojure": [2, "text/x-clojure"], "Java": [8, "text/x-java"], "Go": [6, "text/x-go"], "Plain JavaScript": [4, "text/javascript"], "PHP": [3, "text/x-php"], "Python": [0, "text/x-python"], "Ruby": [1, "text/x-ruby"], "Scala": [5, "text/x-scala"], "VB.NET": [9, "text/x-vb"], "Bash": [11, "text/x-bash"], "Objective-C": [12,"text/x-objectivec"], "MySQL": [13,"text/x-sql"], "Perl": [14, "text/x-perl"], } //create the select element var sel = document.createElement('select'); //foreach key create option element and append to select element for(key in LANGS){ var opt = document.createElement('option'); opt.value = key; opt.innerHTML = key; sel.appendChild(opt); } //once select is poplulated, append document.body.appendChild(sel); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM