将文本放入JavaScript中的文本框中

时间:2011-09-10 17:33:54

标签: javascript arrays text textbox

我是JavaScript的新手,我在将从已创建的数组中读取的信息放入文本框时遇到问题。我正在使用Dashcode,但我正在修改main.js文件中的元素。我创建了一个数组,可以从数组中获取值,我只是无法将信息输入到文本框中。

不起作用的行是:

document.getElementById("text").setAttribute("text",textLocation);

其中text是框的ID,textLocation是我尝试传递到文本框的信息。

如果有人能提供帮助,我们将不胜感激。

其余代码如下。

var n = null;
var textLocation;
var myArray = new Array("info one","info 2","info 3","info 4","info 5","info 6","info 7","info 8","info 9");

function toPreviousImage(event)
{

    var list = document.getElementById("grid").object; 
    var selectedObjects = list.selectedObjects();
    var name = selectedObjects[0].valueForKey("name");
    var textinfo = selectedObjects[0].valueForKey("info"); 

    name= name - 1;

    if(!n || n == undefined){n=name} else {n--}

    textLocation = myArray[n];

    document.getElementById("text").setAttribute("text",textLocation);

1 个答案:

答案 0 :(得分:1)

我认为你要设置的属性是'value',而不是'text'。

document.getElementById("text").setAttribute("value",textLocation);