zoukankan      html  css  js  c++  java
  • 轻松实现二级无刷新联动菜单

    <select id="cate1"></select>
    <select id="cate2"></select>
    <input name="CategoryID" id="categoryid" />
    <script type="text/javascript">
    var cateData=[{id:'1',name:'计算机',child:[{id:'1',name:'程序设计'}]},{id:'2',name:'语言培训',child:[{id:'2',name:'英语培训'},{id:'3',name:'日语'}]}];

    var pCate=document.getElementById('cate1');
    var cCate=document.getElementById('cate2');

    for(var i=0;i<cateData.length;i++){
        
    var op=document.createElement("option");
        op.appendChild(document.createTextNode(cateData[i].name));
        op.setAttribute(
    "value",cateData[i].id);
        pCate.appendChild(op);
    }
    //填充默认子下拉菜单
    addOptions(0);

    //填充指定索引的选项
    function addOptions(index){
        
    //清空所有子分类       
        cCate.innerHTML=null;
        
    var cData=cateData[index].child;//获取子分类
         for(var i=0;i<cData.length;i++){
            
    var op=document.createElement("option");
            op.appendChild(document.createTextNode(cData[i].name));
            op.setAttribute(
    "value",cData[i].id);
            cCate.appendChild(op);
        }
    }
    pCate.onchange
    =function(){addOptions(this.selectedIndex);}
    cCate.onchange
    =function(){
        document.getElementById(
    'CategoryID').value=
            pCate.options[pCate.selectedIndex].value
    +"-"+
            cCate.options[cCate.selectedIndex].value;
    }
    </script>
  • 相关阅读:
    JSON
    邮箱正则表达式
    聚聚科技---PHP开发笔试题及答案
    PHP字符串左边补0,字符串右边补0
    CSS3实现带阴影的弹球
    背景颜色渐变
    CSS3---滤镜
    CSS3裁剪与遮罩解析
    CSS3---混合模式
    使用CSS3制作各种形状
  • 原文地址:https://www.cnblogs.com/newmin/p/1538816.html
Copyright © 2011-2022 走看看