实现jsp页面二级下拉框联动,实时读取数据库数据

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

在一个jsp页面实现二级下拉框联动,实时读取数据库数据,这个方法非常有用,只需要修改很小的地方就可以使用。设计的文件,serch.jsp,main.js,bytetostr.js,

先讲一下main.js,这是javascript,其中注意修改jsp页面名称。


 functionfindObject(fName,initValue)...{
  varxmlhttp=newActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open("POST","searchmx.jsp?findObject="+fName+"&initValue="+initValue,false);//注意修改jsp页面
  xmlhttp.send();
  document.getElementById(fName).innerHTML=bytes2BSTR(xmlhttp.responsebody);//bytes2BSTR函数在bytetostr.js中
 }

 Functionbytes2BSTR(vIn)
 dimi
 strReturn=""
 Fori=1ToLenB(vIn)
 ThisCharCode=AscB(MidB(vIn,i,1))
 IfThisCharCode<&H80Then
 strReturn=strReturn&Chr(ThisCharCode)
 Else
 NextCharCode=AscB(MidB(vIn,i+1,1))
 strReturn=strReturn&Chr(CLng(ThisCharCode)*&H100+CInt(NextCharCode))
 i=i+1
 EndIf
 Next
 bytes2BSTR=strReturn
EndFunction

接下来就是serch.jsp页面,只需要修改div中的id为你想要的名字,再将一开始的包更换一下,已经sql语句改下就可以了,有不明白的可以参与讨论



<%@pagecontentType="text/html;charset=gb2312"language="java"import="com.sjth.zdsygl.vo.*,com.sjth.zdsygl.biz.*,java.util.*,com.sjth.zdsygl.jdbc.*,java.sql.ResultSet"%>//导入相应的包
<%
//这部分代码用于执行查询数据库,返回字符串
 if(request.getParameter("findObject")!=null)...{
   if(request.getParameter("findObject").equals("hy_dm"))...{
     DBConnectconn=null;
     ResultSetrs=null;


     try...{                   
       out.print("<selectname='hy_dm'οnchange="javascript:findObject('hymx_dm',this.value)">");
       Stringsql="select*fromdm_hygroupbyleft(hy_dm,7)";
       conn=newDBConnect();
       conn.setPstmt(sql);
       rs=conn.executeQuery(sql);
       while(rs.next())...{
         out.print("<optionvalue='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
       }    
       out.print("</select>");
    }
    catch(Exceptione)...{                   
    }
    finally...{
         try...{
           if(rs!=null)
               rs.close();
           if(conn!=null)
               conn.close();
         }
         catch(Exceptione)...{
           e.printStackTrace();
         }
    }
  }
   if(request.getParameter("findObject").equals("hymx_dm"))...{
     DBConnectconn=null;
     ResultSetrs=null;
     try...{                   
       out.print("<selectname='hymx_dm'>");
       Stringsql=null;
       if(request.getParameter("initValue").equals(""))...{
         sql="select*fromdm_hy";
       }
       else...{
         sql="select*fromdm_hywherehy_dmlike'"+request.getParameter("initValue")+"%'";


       }
       conn=newDBConnect();
       conn.setPstmt(sql);
       rs=conn.executeQuery(sql);
       while(rs.next())...{
         out.print("<optionvalue='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
       }    
       out.print("</select>");
    }
    catch(Exceptione)...{                   
    }
    finally...{
         try...{


           if(rs!=null)
               rs.close();
           if(conn!=null)
               conn.close();
         }
         catch(Exceptione)...{
           e.printStackTrace();
         }
    }
  }
  return;
 }
%>
<html>
<head>
   <linkhref="CSS/table.css"type="text/css"rel="stylesheet">
   <scriptlanguage=vbscriptsrc="css/bytetostr.js"></script>


   <scriptlanguage=javascriptsrc="css/main.js"></script>//导入两个js文件
</head>
<body>
     <div>所属行业:</div>
     <divid="hy_dm"></div>//用来显示返回的字符串
     <div>所属明细行业:</div>
     <divid="hymx_dm"></div>//用来显示返回的字符串
</body>
</html>
<scriptlanguage="javascript">
      findObject("hy_dm","");
      findObject("hymx_dm","");//这两条是页面执行是调用
</script>

欢迎大家讨论

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页