我使用这个 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">
有什么问题吗。我向它传递了正确的参数??
最佳答案
粗略一看,似乎是
var selObj = document.getElementById(idName);
document.getElementById(idName)
未返回任何内容(或更准确地说返回 null
)。
我的猜测是idName
的值不匹配。我首先要确保在 document.getElement
语句之前这个变量的值到底是什么(为了彻底起见,我还会检查非打印字符)。
关于javascript - 空对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1162100/