我正在制作一个小型 Spring MVC 项目并保存用户数据。在页面中,当我单击从后端编辑 Country 数据填充并应用 Ajax Onchange 事件时。
当管理员选择国家时,追加州列表,当管理员选择州时,追加地区列表等等。但是当我点击编辑国家数据和无州时,附加地区数据。我还尝试在负载时运行 Ajax。谁能帮我?
我的代码如下所示:
用户控制器.java:
@RequestMapping(value = "/editApplicant/{id}", method = RequestMethod.GET)
public String editUser(@PathVariable("id") int id, ModelMap modelMap) {
UserDataPojo userDataPojo = userDataService.getDetailsById(id);
List<DestinationPojo> destinationPojos = destinationService.getAllCountries();
modelMap.addAttribute("countries", destinationPojos);
modelMap.addAttribute("applicantData", userDataPojo );
if (applicantDataPojo == null) {
modelMap.addAttribute("Msg", "User is Not Found");
}
return "updateuser";
}
并更新user.jsp
<label>Country*</label>
<form:select class="form-control"
onChange="AjaxCallStateperma()" id="parmanentCountry"
path="parmanentCountry">
<form:option value="${null}">Select</form:option>
<form:options items="${countries}" itemLabel="countryName"
itemValue="countryId" />
</form:select>
<label>State*</label>
<form:select class="form-control"
onChange="ajaxcalldistrictperma()" id="parmanentState"
path="parmanentState">
<form:option value="${null}">Select</form:option>
</form:select>
我的ajax是
<script type="text/javascript">
// business Address to list start
function AjaxCallState() {
var country = $('#businessCountry').val();
console.log(country);
$.ajax({
url : "${pageContext.request.contextPath}/loadState",
async : false,
type : "GET",
data : "state=" + country,
contentType : "application/json;charset=UTF-8",
dataType : "json",
success : function(data) {
$('#businessState').empty();
$('#businessState').append(
'<option value="null">Select State</option>');
$.each(data, function(i, value) {
$('#businessState').append(
$('<option>').text(value.stateName).attr(
'value', value.stateId))
});
}
});
}