无法获得数组中的长度

时间:2011-03-29 08:41:37

标签: javascript

我甚至无法得到警报提示。我在IE9上做了什么错?

<script type="text/javascript">
    var countrieslist=document.sc.s1
    var subjlist=document.sc.s2

    var subj=new Array()
    subj[0]=""
    subj[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
    subj[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
    subj[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]

function updateSubj(selectsubj) {
    if (selectsubj>0){
        for (i=0; i<subj[selectsubj].length; i++) {
        alert("ss");
            //s2.options[subjlist.options.length]=new Option(subj[selectsubj][i].split("|")[0], subj[selectsubj][i].split("|")[1])
        }
    }
}
</script>

1 个答案:

答案 0 :(得分:0)

我应该检查它是否是一个数组。

function update (i) {
    if (subj[i] instanceof Array)
        for(var j in subj[i])
            alert(subj[i][j]);
    else
        alert(subj[i] || 'is undefined');
}

您还可以通过检查长度属性

来检查它是否是可迭代的
if (subj[i].length) // iterate list