我要做省、市、县三级下拉菜单,现在遇到一个这样的问题?我选择省以后会出现城市,但是如果不去选择城市的话,第三级下拉框就动不了了,也就是说要想显示第三级下拉框,一定要动第二级下拉框,总题来了,如果我第二级下拉框只有一个城市,怎么动第三级下拉框呀。代码如下:

解决方案 »

  1.   


    <% 
         ShiModel model = new ShiModel();
         ShiManagerImpl manager = new ShiManagerImpl();
         AreaModel areaModel = new AreaModel();
         AreaManager areaManager = new AreaManagerImpl();
         List dataList = null,areaList=null;
         String sql = "select * from area order by id desc";
         dataList = manager.getShiInfo();
         areaList = areaManager.getAreaList(sql);
    %>
    <script language="javaScript" src="/js/common.js" ></script>
    <script Language="JavaScript"><!--     var onecount;
             onecount=0;
             subcat = new Array();
           <% 
             int count=0;
             while(count<dataList.size()){
                model =(ShiModel) dataList.get(count);
           %>  
           subcat[<%=count%>] = new Array("<%=model.getShiName()%>","<%= model.getShengId()%>","<%= model.getShiId()%>"); 
           <%
           count++;
           }
           %> 
           onecount =<%=count%>;
         //第二级下拉
         function changelocation(locationid)
        {
        document.companyForm.shiid.length = 0; 

        var locationid=locationid;
        var i;     
        for (i=0;i < onecount; i++)
            {
                if (subcat[i][1] == locationid)
                { 
                 document.companyForm.shiid.options[document.companyForm.shiid.length] = new Option(subcat[i][0], subcat[i][2]);
               
                }   
            }
           
           //changelocation1(subcat[i][2]);     
        }  
    //第三级下拉
    var twocount;
    twocount=0;
    subcate1=new Array();
    <%
    count = 0;
    while(count<areaList.size()){
     areaModel = (AreaModel)areaList.get(count);
    %>
    subcate1[<%=count%>]=new Array("<%=areaModel.getAreaName().trim()%>","<%=areaModel.getShiid()%>","<%=areaModel.getId()%>");
        <%count++;}%>
        twocount=<%=count%>
        function changelocation1(shiid){
          document.companyForm.quid.length=0;
          var shiid=shiid;
          var i;
          for(i=0;i<twocount;i++){
            if(subcate1[i][1]==shiid){
             document.companyForm.quid.options[document.companyForm.quid.length]=new Option(subcate1[i][0],subcate1[i][2]);
            }
          }
        }     
      

  2.   


      <% 
                                        ShengModel smodel = new ShengModel();
                                        ShengManagerImpl smanager = new ShengManagerImpl();
                                        List arrayList = smanager.getShengInfo();
                                        if(arrayList==null||arrayList.size()<1){
                                    %>
                                    <tr><td>请先添加栏目</td></tr>
                                    <%}else{%>
                                      <tr> 
                                        <td width="14%">所属省份:</td>
                                        <td colspan="2"> 
                                 
                                          <select style="width:90px;height:20px;" name="shengid" size="1" id="shengid" onChange="changelocation(document.companyForm.shengid.options[document.companyForm.shengid.selectedIndex].value)">
                                           <option selected="selected" value="0">请选择省份</option>
                                           <%for(int i=0;i<arrayList.size();i++){
                                                 smodel =(ShengModel) arrayList.get(i);  
                                           %>
                                            <option  value="<%=smodel.getShengId()%>"><%=smodel.getShengName()%></option>
                                           <%}}%>
                                          </select>
                                          所属城市: 
                                          <select style="width:90px;height:20px;" name="shiid" id="shiid" onChange="changelocation1(document.companyForm.shiid.options[document.companyForm.shiid.selectedIndex].value)">
                                           <option selected="selected" value="0">请选择城市</option>
                                         </select>
                                          所属县区:
                                          <select style="width:90px;height:20px;" name="quid">
                                           <option selected="selected" value="0">请选择县区</option>
                                          </select>
                                          <font color="#FF0000">&nbsp;</font></td>
                                      </tr>
      

  3.   

    动第二个,可是怎么才能获得那个shiid号呢。
      

  4.   


     所属县区:
                                          <select style="width:90px;height:20px;" name="quid">
                                           <option selected="selected" value="0">请选择县区</option>
                                          </select>
    <script>
    //在这加上下面的内容试试,或者再加上其他相关的东东changelocation1(document.companyForm.shiid.options[document.companyForm.shiid.selectedIndex].value)
    </script>
      

  5.   


    一般情况下,第一个换选了,第二个下拉框(B)会选择第一个内容,你直接那个B的第一个id传过去就是了这和六楼方法不一样
      

  6.   

    你可以改成这样试试  
    所属城市: 
     <select style="width:90px;height:20px;" name="shiid" id="shiid" onPropertyChange="changelocation1(document.companyForm.shiid.options[document.companyForm.shiid.selectedIndex].value)">
                                           <option selected="selected" value="0">请选择城市</option>
        </select>
      

  7.   


      //第二级下拉
         function changelocation(locationid)
                    {
                    document.companyForm.shiid.length = 0; 
                
                    var locationid=locationid;
                    var i;                
                    for (i=0;i < onecount; i++)
                        {
                            if (subcat[i][1] == locationid)
                            { 
                             document.companyForm.shiid.options[document.companyForm.shiid.length] = new Option(subcat[i][0], subcat[i][2]);
                           
                            }   
                        }
                       
                       //changelocation1(subcat[i][2]);     
                    } 我知道你的意思,我怎么捕获到第一个i呢,然后调用changelocation1(subcat[i][2]);     
      

  8.   

    选择省的时候,有个默认市的,但我怎么才能捕获这个i值呢,就是说changelocation1(subcat[i][2]);     
    我应该放在哪里呢?
      

  9.   

    选择省的时候,省的id是不是已经知道了?
    与这个省id对应的市的数组是不是可以确定了,
    这个市数组确定了,那么,取这个市的数组的第一个值的id,传给选择市的那个函数,你看看行得通不六楼的方法是以为第一次出现的情况,在你这种情况下不可用
      

  10.   

    <select style="width:90px;height:20px;" name="shiid" size="1" id="shiid" onChange="alert('你改变了选择');">
       <option selected="selected" value="0">请选择城市</option>
       <option value="1">厦门市</option>
       </select>   加了“请选择城市”,这样即使只有“厦门市”一个,也能产生值改变事件了