我需要使用 ajax 从数据库中填充一个下拉列表,我使用两个下拉列表,如果选择了第一个下拉值,则第二个下拉值(必须根据第一个下拉列表中选择的值从数据库中检索)必须有要显示。DAO(数据访问层)作为arraylist对象返回4个结果,但在http responsetext中它打印为对象而不是值。我尝试使用for循环来迭代它,但我无法实现它。请在这方面帮助我。
HTML 代码:
// First Drop Down
Question Field :<select name="ddlAddQuestionField" id='ddlAddQuestionField' onchange="getFieldPosition()">
<option value=''>Select Question Field</option>
<option value='Security Question'>Security Question</option>
<option value='Personal Info'>Personal Info</option>
</select>
// Second DropDown
User Field Position:<select name="userFieldPosition" id="userFieldPosition" disabled="disabled"> </select>
Javascript代码
function getFieldPosition(){
var fieldName =$("#ddlAddQuestionField").val();
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("userFieldPosition").disabled=false;
alert(xmlhttp.responseText);
var response =xmlhttp.responseText;
for(var i=0;i<response.length;i++) {
var elements = "<option value='"+response[i]+"'>"+response[i]+"</option>";
$("#userFieldPosition").append(elements);
}
}
}
xmlhttp.open("POST","ApplicationController",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fieldAction=fieldPosition&fieldName="+fieldName);
}
小服务代码
fieldPositionObj = fieldPositionDaoObj.getFieldPosition(fieldName); //Hitting the Dao
// In dao it returns arraylist object.
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(fieldPositionDaoObj);