我的jQuery是:
$('select[name=select1]').change(function () {
var cod=$(this).val();
if (cod>0) {
$.ajax({
url:"Load.do",
data: "cod="+cod,
success: function (response) {
$('#info').html(response); }
});
}
});
在 struts-config.xml 中,我有:
<action path="/Load" scope="request" type="mypackage.Load"></action>
类 Load 是一个动作:
public class Load extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// get the connection
stmt = (Statement) con.createStatement();
String cod = request.getParameter("cod");
String select = new String("Select firstCode, name from mytable where secondCode = '");
select = select.concat(cod);
select = select.concat("';");
rs = stmt.executeQuery(select);
PrintWriter out = response.getWriter();
while (rs.next()) {
out.print("<html:option value='");
out.print(rs.getString("firstCode"));
out.print("'>");
out.print(rs.getString("name"));
out.println("</html:option>");
out.flush();
}
return null;
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try { rs.close(); } catch (SQLException e) {}
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) {}
stmt = null;
}
if (con != null) {
try { con.close(); } catch (SQLException e) {}
con = null;
}
}
return null;
}
}
数据被检索,但它们没有打印在信息 div 中。
在我的 .jsp 页面中,我要填写的选择是:
<html:select property="prop1">
<html:option value="0">Choose:</html:option>
<div id="info"></div>
</html:select>