简体   繁体   English

当“选项”不是静态时,如何引用“选择”列表的选定值?

[英]How do I make a reference to the selected value of a “select” list when the “options” are not static?

I have a simple question. 我有一个简单的问题。 I have a select list like this: 我有一个这样的选择列表:

var myarray = ["one", "two", "three"];
var container = document.createElement("select");  
for (var i = 0; i < myarray.length; i++) {
    var element = document.createElement("option");
    var textlabel = document.createTextNode(myarray[i]);

    if (element.nodeValue == "two") { 
        element.selected = true;
    }

    element.appendChild(textlabel); 
    container.appendChild(element);
}
document.body.appendChild(container);

I have two questions about it: 我有两个问题:

1) I am pretty sure that the element that should be selected right now is "two"... isn't it? 1)我很确定现在应该选择的元素是“两个”......不是吗?

2) Since the option elements are being created dinamically inside a loop (there are no three different option variables for me to play with, but just one that gets renewed as the loop goes forward), how do I reference the selected one for future uses? 2)由于option元素是在循环内部创建的(我没有三个不同的选项变量供我使用,但只有一个随着循环前进而更新),我如何引用所选的一个以供将来使用?

For example, imagine that later on I get user input, and according to that input I want that this list has as a selected item, "three". 例如,想象一下,稍后我会得到用户输入,并根据该输入我希望此列表具有选定项目“三”。

Thank you for any help! 感谢您的任何帮助! Here is the fiddle if you want to use it... 如果你想使用它,这是小提琴 ......

只需在for循环中更改以下内容即可解决选择问题:

if (myarray[i] == "two")

1) I am pretty sure that the element that should be selected right now is "two"... isn't it? 1)我很确定现在应该选择的元素是“两个”......不是吗?

No, it's not: you check element.nodeValue, while in fact you should've been checking textLabel 's one - or just the content itself: 不,它不是:你检查了element.nodeValue,而实际上你应该检查textLabel - 或者只是内容本身:

if (myarray[i] === 'two') {
  element.selected = true;
}

2) Since the option elements are being created dinamically inside a loop (there are no three different option variables for me to play with, but just one that gets renewed as the loop goes forward), how do I reference the selected one for future uses? 2)由于选项元素是在循环内部创建的(我没有三个不同的选项变量供我使用,但只有一个随着循环前进而更新),我如何引用所选的一个以供将来使用?

See, <select> elements has two useful properties: options (which contains all the options in it, and is updated dynamically) and selectedIndex . 请参阅<select>元素有两个有用的属性: options (包含其中的所有选项,并动态更新)和selectedIndex You can combine them to get the selected option: 您可以将它们组合在一起以获得所选的选项:

container.addEventListener('change', function() {
   console.log(this.options[this.selectedIndex]);
}, false);

But if what you want is to know the value of selected element, that's even easier - with container.value . 但是如果你想要知道所选元素的价值 ,那就更容易了 - 使用container.value

For example, imagine that later on I get user input, and according to that input I want that this list has as a selected item, "three". 例如,想象一下,稍后我会得到用户输入,并根据该输入我希望此列表具有选定项目“三”。

That's piece of cake if you know the position of the option that corresponds to this: just use selectedIndex property again: 如果您知道与此对应的选项的位置,这是一块蛋糕:再次使用selectedIndex属性:

container.selectedIndex = 3;

Try to use console.log (on chrome or firefox with firebug) to debug your script : 尝试使用console.log (在chrome或firefox上使用firebug)来调试脚本:

Try this : 试试这个

var myarray = ["one", "two", "three"];
var container = document.createElement("select");
container.id = "mySelect" ;  
    for (var i = 0; i < myarray.length; i++) {
        var element = document.createElement("option");
        var textlabel = document.createTextNode(myarray[i]);

        element.appendChild(textlabel);   

        if (element.value == "two") { 
            element.selected = true;

        }

        container.appendChild(element);
    }
        document.body.appendChild(container);

in order to refer to your selected element you should give your select element an id and access to it like below: 为了引用您选择的元素,您应该为您的select元素提供一个id并访问它,如下所示:

el = document.getElementById('mySelect');
el.selectedIndex ; // give you the selected index
el.options[el.selectedIndex]; // give you the value

暂无
暂无

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

相关问题 如何从长选择选项列表中拉出所选值? - How to pull selected value form long select options list? 如果未选择复选框中的任何一个,则如何将其设置为none? - When non of the options are selected for the checkboxes, how to make the value none? 如何从 dropdwon 创建所选选项的列表 - How do I create a list of the selected options from a dropdwon 我如何使getSelectedCurrency仅在使用选项填充了select元素时运行 - How do I make getSelectedCurrency run only when the select element has been populated with options 如果选择了另一个选择下拉列表中的一个选项,如何显示选择下拉列表 - How do I display a select dropdown list when one option on another select dropdown is selected 我如何使html select选项在Meteor中工作? - How do I make the html select options work in Meteor? 使用带有 datalist 的输入作为选择 - 如果已经选择了一个值,我如何打开一个完整的值列表? - Using input with datalist as select - how do I open a full list of values if a value is already selected? 当我单击带有数据的按钮时,我该如何执行此操作,它将使用 NextJS 在 select 选项数据上选择值? - How can I do this when I clicked button with data it will selected value on select option data with NextJS? 如何在选择列表中发布所有选项? - How do I POST all options in a select list? 我如何得到我的 <select>在AngularJS中列出以仅在未选择任何值时显示消息? - How can I get my <select> list in AngularJS to show a message ONLY when no value is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM