1

hello everyone I am trying to make a script that steps through a list and appends a score to each record returned by the query. this code below is what's happening in PHP; a input is created where I store the number that's going to be processed in a string; below that there is a span element that displays the text score and another span that contains the actual score. Finally there's an echo that showcases the values generated which look like: score_pad0, score_pad1, score_pad2

echo "<input type='hidden' id='fetch_array' value=".$score_board." />";
echo "<span>score: </span><span id='score_pad$score_board'></span><br />";
echo "score_pad$score_board";

I want to grab the value from fetch_array in javascript. I use the following code for that
var array_id = document.getElementById('fetch_array'); this however returns null when I run it. What am I doing wrong? I've tried to read it out by adding the .value extension. but it only crashed my javascript code.

the onload event is triggered by the a function

function initialize(numbers_rows)
{
for(var i=0; i < numbers_rows; i++)
{   
    var node=document.createElement("span");
    node.id="scoreLabel";
    document.getElementById("score_pad"+array_id).appendChild(node);
}
}
4

2 回答 2

1

您的 javascript 代码执行时,您的 DOM 可能还没有准备好。作为一个试验做<body onload='myFunction()'>和包裹你的document.getElementById内心myFunction()

如果您希望有多个处理 onload 事件的处理程序确实有效,则您需要将 addEventListener DOC用于非 IE 浏览器和attachEventIE。

如果您可以使用 jQuery,那么将您的 javascript 代码包装起来

$().ready(function () {
 // Your Code Goes here &Will be executed after DOM is ready
});

检查这个问题

于 2012-06-14T11:58:19.040 回答
0

利用

var array_id = document.getElementById('fetch_array').value;
于 2012-06-14T11:31:32.890 回答