空对象问题

时间:2009-07-21 22:33:08

标签: javascript

我正在使用这个JS脚本进行多个国家选择,我从firebug收到错误。

selObj is null
[Break on this error] selObj.options[0] = new Option('Select Country','');

相关代码如下:

function populateCountry(idName) {
 var countryLineArray = country.split('|'); // Split into lines

 var selObj = document.getElementById(idName);

 selObj.options[0] = new Option('Select Country','');
 selObj.selectedIndex = 0;


 for (var loop = 0; loop < countryLineArray.length; loop++) {

 lineArray = countryLineArray[loop].split(':');

 countryCode = TrimString(lineArray[0]);

完整的脚本can be found here

这是我在HTML中使用它的方式:

<select id="billCountrySelect" onchange="updateState('billCountrySelect')" name="bill_country">

问题是什么我正在传递它正确的参数??

1 个答案:

答案 0 :(得分:2)

从粗略的一瞥看来,

“var selObj = document.getElementById(idName);”

“的document.getElementById(idName);”没有返回任何东西(或者更确切地说是返回null)。

我的猜测是idName的值不匹配。我首先要确保在document.getElement语句之前确切地确定该变量的值是什么(我还要检查非打印字符是否彻底)。

相关问题