jQuery 下拉選單選擇 載入 (練手例項)

springlin2011發表於2012-01-11

<html>   
<head>   
<title>Next join Test</title>   
<style type="text/css">   
    .font-color{   
        width:150px;   
        height: 200px;   
        color: rgb(79,129,189)   
    }   
    #div1{border:1px solid #000; width: 50%; position:relative; top: 20%; background-color: #ccc;}   
    #province{position:relative; top: -170px; left: 10px;}   
    #div2{border:1px solid #000; width: 50%; position:relative; top: 19%; background-color: #ccc;}   
</style>   
<script type="text/javascript" src="lib/jquery-1.4.2.js"></script>   
<script type="text/javascript">   
  
var simpleNodes =[   
                { id:1, pId:0, name:"手機", ename:"Mobile", open:true},   
                { id:11, pId:1, name:"諾基亞", ename:"Nokia"},   
                { id:111, pId:11, name:"C6(音樂版)", ename:"C6(Music)"},   
                { id:112, pId:11, name:"X6(導航版)", ename:"X6(GPS)"},   
                { id:113, pId:11, name:"5230(世博版)", ename:"5230(SB)"},   
                { id:114, pId:11, name:"N97mini", ename:"N97mini"},   
                { id:12, pId:1, name:"三星", ename:"Samsung"},   
                { id:121, pId:12, name:"I9000(聯通版)", ename:"I9000(Unicom)"},   
                { id:122, pId:12, name:"I9000(移動版)", ename:"I9000(China Mobile)"},   
                { id:123, pId:12, name:"Galaxy Naos", ename:"Galaxy Naos"},   
                { id:124, pId:12, name:"Fascinate", ename:"Fascinate"},   
                { id:13, pId:1, name:"索愛", ename:"Sony Ericsson"},   
                { id:131, pId:13, name:"U1i(Satio)", ename:"U1i(Satio)"},   
                { id:132, pId:13, name:"U5i(Vivaz)", ename:"U5i(Vivaz)"},   
                { id:133, pId:13, name:"X10i", ename:"X10i"},   
                { id:134, pId:13, name:"Aino mini", ename:"Aino mini"}];   
  
//初始化載入province   
function province(){   
    for(i=0; i < simpleNodes.length; i++ ){   
        if(simpleNodes[i].pId == "1"){   
            var newoption = document.createElement("option");   
            newoption.text = simpleNodes[i].name;   
            newoption.value = simpleNodes[i].id;   
            form1.province.options.add(newoption);   
        }   
    }   
}   
  
$(function(){   
       
    $("#province").change(function(){   
        var value = this.value;   
        var len = form1.city.length;   
        if(len > 0){   
            $("#city option").remove();   
        }   
        for(i=0; i < simpleNodes.length; i++ ){   
            if(simpleNodes[i].pId == value){   
                //效率過低   
                //$("#city").append("<option value="+simpleNodes[i].id+">"+simpleNodes[i].name+"</option>");   
                //$('<option/>',{   
                //  value:simpleNodes[i].id,   
                //  text:simpleNodes[i].name}).appendTo("#city");   
                   
                //JS   
                var newoption = document.createElement("option");   
                newoption.text  = simpleNodes[i].name;   
                newoption.value = simpleNodes[i].id;   
                form1.city.options.add(newoption);   
            }   
        }   
    });   
  
    $("#city").dblclick(function(){   
        var value = $("#city option:selected").val();   
        var text = $("#city option:selected").text();   
        var flag = false;   
        var len = $("#selcity option").length;   
        //alert(len);   
        if(len > 0){   
          $("#selcity option").each(function(i){   
            if(value == $(this).val()){   
                flag = true;   
                alert("已選擇,請勿重選!");   
                return false;   
             }   
           });   
           if(!flag){   
                //不刪除原節點   
             //$("#selcity").append("<option value="+ value +">"+ text +"</option>");   
             $("#city option:selected").appendTo("#selcity");   
           }    
         }else{   
            //不刪除原節點   
            //$("#city option:selected").append("<option value="+ value +">"+ text +"</option>");   
            $("#city option:selected").appendTo("#selcity");   
         }   
    });   
       
    $("#selcity").dblclick(function(){   
        $("#selcity option:selected").remove();   
        //還原   
        //$("#selcity option:selected").appendTo("#city");   
    });   
       
});   
</script>   
</head>   
  
<body onload="javascript:province();">   
<center>   
<div id="div2">   
    <span><font color="blue"><strong>jQuery + JS 下拉框載入選擇</strong></font></span>    
</div>   
<div id="div1">   
<form id="form1" name="form1">   
    <select id="province" name="province">   
        <option value="N" selected="selected">請選擇</option>   
    </select>   
    &nbsp;&nbsp;   
    <select size="10" class="font-color" id="city" name="city" style="width:150px"></select>   
    <select multiple="multiple" class="font-color" id="selcity" name="selcity"></select>   
  
</form>   
<div>   
</center>   
</body>   
  
</html>  

 

相關文章