I'm exercising with Ajax, Jquery and Jsp but I have a problem, I would like to display nested select, the second depends on the first, but I can not see anything when i do my choose in the first select. I hope you can help me, here is the code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="js/jquery-2.1.3.min.js"></script>
<script src="js/city.js"></script>
</head>
<body>
<div id="state"> State
<select id="state">
<option id="IT" value="italy" selected="selected">Italy</option>
<option id="FR" value="france">France</option>
<option id="SP" value="spain">Spain</option>
</select>
</div>
<div id="city"> City
<select id ="city"></select>
</div>
</body>
</html>
city.js
$(document).ready(function () {
$("#state").onchange(function () {
var state = $("select#state option:selected").val();
$.ajax({
type: "POST",
url: 'jspState.jsp',
data: {stateName: state},
success: function (result) {
$("#city").html(result);
},
error: function (e) {
alert('Errore');
}
})
})
})
jspState.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% response.setContentType("text/html");
String state = request.getParameter("stateName");
String it = "italy";
String fr = "france";
String sp = "spain";%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js/jquery-2.1.3.min.js"></script>
<script src="js/city.js"></script>
</head>
<body>
city
<select id ="city"> <% if (0==it.compareTo(state)) { %>
<option value="palermo">Palermo</option>
<option value="roma">Roma</option>
<option value="milano">Milano</option>
<% } if (0==fr.compareTo(state)) { %>
<option value="paris">Paris</option>
<option value="marsille">Marsille</option>
<option value="nice">Nice</option>
<% } if (0==sp.compareTo(state)) { %>
<option value="madrid">Madrid</option>
<option value="barcelona">Barcelona</option>
<option value="sivilla">Sivilla</option>
<% } %>
</select>
</body>
</html>