2011-10-07 67 views
0

我有一个设置selected = true的函数。我有一个国家的下拉列表。每个选项都有IDS美国,GER这样......Javascript document.getElementById(arg) - 对象为空或未定义

function nation(arg) 
{ 
    if (arg == '<?php echo $this->escape($this->user->get('country'));?>') 
     document.getElementById(arg).selected = true; 
} 

window.onload = nation('<?php echo $this->escape($this->user->get('country'));?>'); 

我'确保PHP代码返回我正确的国家,但JavaScript调试器说:

SCRIPT5007: Unable to set value of the property 'selected': object is null or undefined 

这里有什么问题吗?

+1

在客户端发送相关生成的Javascript代码,而不是服务器端脚本。 – duri

回答

3

要调用nation立即,然后它的返回值赋给onload,所以你几乎可以肯定,称这是之前的<select>已经被添加到DOM。

您需要指定一个函数。

window.onload = function() { 
    nation('<?php echo $this->escape($this->user->get('country'));?>'); 
}; 
+0

谢谢!这有助于...... –

相关问题