JSP、Java实现选择框多级连动(2)

分类: Jsp   出处:iocblog整理  更新时间:2009-07-24   添加到收藏  

        /**
         * 获取一个省份下城市选项的html代码串
         * @param pcode 省份
         * @param sect 默认选中的城市代码
         * @return 城市选项的html代码串
         */
        public static string getcityoptions(string pcode, string sect) {
            string optionhtml = "";
            connection conn = getconnectionbyjdbc();
            resultset rs;
            string sqlx = "select t.code,t.name from city t where t.code = ? order by t.code asc";
            try {
                //开始一个事务(注意关闭事务)
                conn.setautocommit(false);
                //创建一个jdbc声明
                preparedstatement pstmt = conn.preparestatement(sqlx, resultset.type_forward_only, resultset.concur_read_only, resultset.close_cursors_at_commit);
                //设置预定义参数
                pstmt.setstring(1, pcode);
                rs = pstmt.executequery();
                while (rs.next()) {
                    string code = rs.getstring("code");
                    string name = rs.getstring("name");
                    if (sect.equals(code)) {
                        optionhtml = optionhtml + "<option value="" + code + """ + " selected>" + code + "-" + name + "</option> ";
                    } else {
                        optionhtml = optionhtml + "<option value="" + code + "">" + code + "-" + name + "</option> ";
                    }
                }
                //提交事务
                conn.commit();
                //关闭连接等资源(注意先后顺序)
                rs.close();
                pstmt.close();
                conn.close();
            } catch (sqlexception e) {
                system.out.println(e.getmessage());
                e.printstacktrace();
            } finally {
                //预防性关闭连接(避免异常发生时在try语句块关闭连接没有执行)
                try {
                    if (conn != null) conn.close();(www.iocblog.net 文章来源)
                } catch (sqlexception e) {
                    system.out.println(e.getmessage());
                    e.printstacktrace();
                }
            }
            return optionhtml;
        }
    }
 (www.iocblog.net 文章来源)

   2、测试用的页面
    <%--
      File Name:    TestCheckBoxLinks.jsp
      Created by:   IntelliJ IDEA.
      Copyright:    Copyright (c) 2003-2006
      Date:         2007-3-6 15:14:33
      Readme:       JSP、Java实现选择框连动,实现页面输入信息回填功能。
    --%>
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ page import="test.TestDBUitl" %>
    <%
        String rootPath = request.getContextPath();
        String topage = rootPath + "/pages/test/TestCheckBoxLinks.jsp";
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String stype = request.getParameter("stype");
        String author = request.getParameter("author");

        if (province == null || "null".equals(province)) province = "";
        if (city == null || "null".equals(city)) city = "";
        if (stype == null || "null".equals(stype)) stype = "";
        if (author == null || "null".equals(author)) author = "熔  岩";
    %>
    <html>
    <head>
        <title>JSP、Java实现选择框连动,实现页面输入信息回填功能。</title>
    </head>
    <body>
    <form method="post">
        <table>
            <tr>
                <td>省 份:</td>
                <td>
                    <select name="province" onchange="getallpames(this)">
                        <option value="" tourl="/zfvims/pages/test/TestCheckBoxLinks.jsp">不限制</option>
                        <%=TestDBUitl.getProvinceOptions(province, topage)%>
                    </select>
                </td>
                <td>城 市:</td>
                <td>
                    <select name="city">
                        <option value="">不限制</option>
                        <%=TestDBUitl.getCityOptions(province, city)%>
                    </select>
                </td>
            </tr>
            <tr>
                <td>类 型:</td>
                <td>
                    <select name="stype">
                        <option value="04">A</option>
                        <option value="07">B</option>
                        <option value="11">C</option>
                        <option value="13">D</option>
                    </select>
                </td>
                <script language="javascript">
                    var a = document.all.stype.options;
                    var n = a.length;
                    for (var i = 0; i < n; i++) {
                        var b = document.all.stype.options[i].value;
                        if (b == "<%=stype%>") {
                            document.all.stype.options[i].selected = true;
                        }
                    }
                </script>
                <td>作 者:</td>
                <td>
                    <input type="text" name="author" value="<%=author%>" maxlength="20">
                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td colspan="2"></td>
                <td><input type="button" name="CX" value="查询"></td>
                <td><input type="reset" value="重置"></td>
                <td colspan="2"></td>
            </tr>
        </table>
    </form>
    </body>
    </html>
    <script type="text/javascript">
        function getallpames(obj) {
            var pames = "&province=" + document.all.province.value;
            pames += "&city=" + document.all.city.value;
            pames += "&stype=" + document.all.stype.value;
            pames += "&author=" + document.all.author.value;
            var tourl = obj.options[obj.selectedIndex].tourl + pames;
            //alert(tourl);  //打印转向地址的开关
            window.location = tourl;
        }
    </script>

上一页 [1] [2]



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。