[英]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)来调试脚本:
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.