简体   繁体   English

使用phantomsjs,如何获取值并将其分配给数组的元素?

[英]Using phantomsjs, how to get a value and assign it to an element of an array?

The following piece of code when ran produces "undefined". 下面的代码在运行时会产生“未定义”。

var getList = document.getElementById('lbExercises').option;
console.log(getList);

The html i wish to get the data from is as follows: 我希望从中获取数据的html如下:

<select id="lbExercises" class="listBox" style="height:400px;width:350px;"                
name="lbExercises" size="4">
<option value="1270">Value I want to Extract 2</option>

The issue is with my JS code - if it helps im running my code by using phantomsjs filename.js 问题出在我的JS代码上-如果它通过使用phantomsjs filename.js帮助我运行我的代码

Thanks. 谢谢。

You could get the first value of the first option like this: 您可以像这样获得第一个选项的第一个值:

var getList = document.getElementById('lbExercises');
console.log(getList.options[0].value);

See jsFiddle: 参见jsFiddle:

http://jsfiddle.net/3LLBS/ http://jsfiddle.net/3LLBS/

This is how you would assign it to an element of an array: 这是将其分配给数组元素的方式:

var anArray = new Array();
anArray[0] = 123;
anArray[1] = 456;
anArray[2] = 789;

// assign the new value
anArray[1] = getList.options[0].value;

console.log(anArray[1]);

See jsFiddle: 参见jsFiddle:

http://jsfiddle.net/3LLBS/1/ http://jsfiddle.net/3LLBS/1/

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

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