0

我正在尝试填充一个下拉菜单。第二个和第三个下拉列表应该在从数据库中检索到之前选择的下拉值后显示值。任何人都可以指出我的代码中的错误。当我运行代码我无法在任何下拉列表中选择任何值。请帮助我。
国家.jsp

状态.jsp

City.jsp 与 state.jsp 几乎相同

我已经在数据库中添加了值。提前致谢

4

1 回答 1

0

请使用预定义的框架,如 JSTL/SQL 和 jQuery.js

这应该有效:

<%--
    Document   : index
    Created on : Jan 23, 2013, 7:43:24 PM
    Author     : Ankit
--%>

<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<sql:setDataSource driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:XE" user="system" password="ankit" var="db" scope="request" />
<sql:query dataSource="${db}" var="countrys" sql="SELECT * FROM country" />
<sql:query dataSource="${db}" var="states" sql="SELECT * FROM state WHERE countryid=${param['countryid']}" />
<sql:query dataSource="${db}" var="cities" sql="SELECT * FROM city WHERE cityid=${param['cityid']}" />
<!DOCTYPE html>
<html>
    <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <script type="text/javascript" language="javascript" src="jquery.js"></script>
    </head>
    <body>
        <form>
        <select name="countryid" id="countrylist" onclick="jQuery('#statelist').load('?countryid='+jQuery(this).val(),'#statelist');">
            <c:forEach items="${countrys}" var="country">
                <option value="${country.id}">${country.name}</option>
            </c:forEach>
        </select>
        <select name="stateid" id="statelist" onclick="jQuery('#citylist').load('?cityid='+jQuery(this).val(),'#citylist');">
            <c:forEach items="${states}" var="state">
                <option value="${state.id}">${state.name}</option>
            </c:forEach>
        </select>
        <select name="cityid" id="citylist">
            <c:forEach items="${cities}" var="city">
                <option value="${city.id}">${city.name}</option>
            </c:forEach>
        </select>
        <button>Finish</button>
        </form>
    </body>
</html>
于 2013-01-28T15:17:30.803 回答