程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> select-javascript選擇框的問題

select-javascript選擇框的問題

編輯:編程綜合問答
javascript選擇框的問題

正確的代碼如下,就是不理解為什麼是options[length],

<html>
<head></head>
<title>省市縣關聯菜單</title>


<script>

function Dsy()
{
    this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
    this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
    if(typeof(this.Items[id]) == "undefined") return false;
    return true;
}



var dsy = new Dsy();

dsy.add("0",["安徽","北京","福建"]);

dsy.add("0_0",["安慶","蚌埠","巢湖","池州","滁州","阜陽","合肥","淮北","淮南","黃山","六安","馬鞍山","宿州","銅陵","蕪湖","宣城","亳州"]);
dsy.add("0_0_0",["安慶市","懷寧縣","潛山縣","宿松縣","太湖縣","桐城市","望江縣","岳西縣","枞陽縣"]);
dsy.add("0_0_1",["蚌埠市","固鎮縣","懷遠縣","五河縣"]);
dsy.add("0_0_2",["巢湖市","含山縣","和縣","廬江縣","無為縣"]);

dsy.add("0_1",["北京"]);
dsy.add("0_1_0",["北京市","密雲縣","延慶縣"]);

dsy.add("0_2",["福州","龍巖","南平","寧德","莆田","泉州","三明","廈門","漳州"]);
dsy.add("0_2_0",["長樂市","福清市","福州市","連江縣","羅源縣","閩侯縣","閩清縣","平潭縣","永泰縣"]);
dsy.add("0_2_1",["長汀縣","連城縣","龍巖市","上杭縣","武平縣","永定縣","漳平市"]);
dsy.add("0_2_2",["光澤縣","建陽市","建瓯市","南平市","浦城縣","邵武市","順昌縣","松溪縣","武夷山市","政和縣"]);
dsy.add("0_2_3",["福安市","福鼎市","古田縣","寧德市","屏南縣","壽寧縣","霞浦縣","周寧縣","柘榮縣"]);

 function change(v){
    var str="0";
    alert(v);//一個
    for(i=0;i<v ;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
    alert(str);//2個
    var ss=document.getElementById(s[v]);
    alert(ss.value);
    with(ss){
        length = 0;
        options[0]=new Option(opt0[v],opt0[v]);
        alert(options[0].value);//3個
        if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
        {
            alert(v);//4個
            if(dsy.Exists(str)){
                ar = dsy.Items[str];
                alert(ar);
                for(i=0;i<ar.length;i++) {
                    options[length]=new Option(ar[i],ar[i]); //!!!!***********options[length]在這裡
                }               
                if(v)options[1].selected = true;
            }
        }
        if(++v<s.length) 
        {
            change(v);
        }
    }
}

var s=["province","city","district"];
var opt0 = ["省份","地級市","市、縣級市、縣"];
function setup()
{
    for(i=0;i<s .length-1;i++)
        document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
    change(0);
}
</script> 

<body bgcolor="#E0E0E0" onload="setup()">
多級關聯菜單:
<form name="frm">
<select id="province"><option>省份</option></select>
<select id="city"><option>地級市</option></select>
<select id="district"><option>市、縣級市、縣</option></select>
</form>
</body>
</html>

請問各位老師,options[length] =new Option(ar[i],ar[i])這句話中為什麼不是options[i]=new Option(ar[i],ar[i])?按照我的理解是,每一次for循環都新建了一個select選項,如果options[]裡面是length的話,那麼每次都新建的是options最後一個選項,而沒有建前面的那幾個。麻煩哪位老師解釋一下,或者指點一下options[]真正的含義,是不是我對著它有錯誤的理解。還有with()的作用是什麼?謝謝。

最佳回答:


由於這個函數使用了坑爹坑妹坑X大爺的with語句,導致讀起來不是很好理解。強烈建議你去掉with語句,這樣代碼不至於乍看起來不知所雲。講一下原理:在with中,定義的變量可以不加對象的引用,比如這裡直接引用了ss對象的length屬性.....那個length的意思其實應該是ss.length,options的意思其實是ss.options,而ss.length是一個變化的值,所以每一次給options賦值之後ss.length會發生變化,即在for循環中的options[lenth]表示ss.options[ss.length]。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved