简体   繁体   English

使用Javascript动态添加选择选项

[英]Dynamically Add Select Options with Javascript

So I've been working on trying to populate a select tag options with JavaScript. 因此,我一直在尝试使用JavaScript填充选择标记选项。 I can't seem to figure out why my function isn't working any help would be greatly appreciated. 我似乎无法弄清楚为什么我的功能不起作用,将不胜感激。

My HTML code: 我的HTML代码:

    <select name="options" id="options" style="width: 100px;" onchange="chooseOption(this);">
</select>

And my JavaScript function: 而我的JavaScript函数:

    function chooseOption(){
    var choices = {"Gym","Pool","Sports"};
    var myChoice = "";

  for(i=0; i<=choices.length; i++){
    myChoice += "<option value='"+choices[i]+"'>"+choices[i]+"</option>";
    document.getElementById("options").innerHTML = myChoice;
  }

}

Thanks again 再次感谢

You're attempting to create an object instead of an array here: 您正在尝试在此处创建对象而不是数组:

var choices = {"Gym","Pool","Sports"}; // change {} to [] to create an array

Curly braces - {} are used to denote that you are creating an object. 花括号- {}用于表示您正在创建一个对象。

Brackets - [] are used to denote an array. 方括号- []用于表示数组。

Try populating your select element as it's being created with something like this: 尝试在创建select元素时填充如下所示的内容:

<select name="options" id="options" style="width: 100px;">
<script>
var choices = ["Gym","Pool","Sports"];
var myChoice = "";
for(var i=0; i < choices.length; i++) {
    myChoice += "<option value='"+choices[i]+"'>"+choices[i]+"</option>";
    document.getElementById("options").innerHTML = myChoice;
}
</script>
</select>

I would go with something like. 我会喜欢这样的。 Is not really tested but am pretty sure is more reliable. 尚未经过实际测试,但是可以肯定它更可靠。

var option = document.createElement("option");
  for(i=0; i<=choices.length; i++){
    option.text = choices[i];
    option.value = choices[i];
    document.getElementById("options").appendChild = myChoice;
  }  

Firstly, you have a huge error. 首先,您有一个巨大的错误。

You don't use { and } in javascript to create arrays. 您无需在JavaScript中使用{}来创建数组。

Use: 采用:

var choices = ["Gym","Pool","Sports"];

Here is your final code: 这是您的最终代码:

<script>
    function chooseOption() {
        var choices = ["Gym", "Pool", "Sports"];
        var myChoice = "";

        for (i = 0; i <= choices.length; i++) {
            myChoice += "<option value='" + choices[i] + "'>" + choices[i] + "</option>";
            document.getElementById("options").innerHTML = myChoice;
        }

    }

</script>
<select name="options" id="options" style="width: 100px;" onclick="javascript:chooseOption(this);">
</select>

Update 更新资料

If you want it to work on JSFiddle firstly you need to make your function globally available because JSFiddle runs it at domready . 如果首先希望它在JSFiddle上运行,则需要使函数全局可用,因为JSFiddle是在domready运行它的。 To make it globally available just write it like this: window.choseOption = function() { /* code here */ }; 要使其在全球范围内可用,只需这样编写: window.choseOption = function() { /* code here */ }; .

You should read a bit on DOM events . 您应该阅读有关DOM事件的内容 The change event on that select won't fire up until you have selected something. 在您选择某项内容之前,不会触发该select的更改事件。 And since you have nothing to select the event will not fire. 而且由于您无选择,因此该事件不会触发。

You can run the function at onclick or just run it when the DOM is ready. 您可以在onclick运行该函数,也可以在DOM准备就绪时运行它。

I have updated your fiddle . 我已经更新了你的小提琴

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

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