autocomplete 就类似于百度搜索一样的文本框。当你属于一个a的时候下面会列出关于a的信息。
<input id="schoolName" name="printDto.schoolName" value="${printDto.schoolName}"//这个value是默认学校
需要的js
$(function() { var availableTags = getSchools(); $( "#schoolName" ).autocomplete({ source: availableTags }); });
//判断文本框是否是正确的学校名字。也就是下拉框的内容
function checkShooleNameExist(schoolName){ var url = "getSchool.html?ajax=true"; var params = {'schoolName':encodeURI(schoolName),'now':new Date()} var reulst=[]; $.ajax({ url: url, type: "POST", async:false, data:params, beforeSend: function(XMLHttpRequest){ //ShowLoading(); }, success: function(data, textStatus){ reulst = eval(data); }, complete: function(XMLHttpRequest, textStatus){ //HideLoading(); }, error: function(){ //请求出错处理 } }); return reulst; }
//用ajax读取后台获取到的所有的学校 function getSchools(){ var url = "findSchool.html?ajax=true"; var params = {'schoolName':'','now':new Date()} var reulst=[]; $.ajax({ url: url, type: "POST", async:false, data:params, beforeSend: function(XMLHttpRequest){ //ShowLoading(); }, success: function(data, textStatus){ reulst = eval(data); }, complete: function(XMLHttpRequest, textStatus){ //HideLoading(); }, error: function(){ //请求出错处理 } }); return reulst; }
我这里用了两个一样的方法。只是提交不同。。额。。看上去似乎有点繁琐哦。。没想到别的更好的方法哦。。
页面所需要的js:
<link rel="stylesheet" href="styles/jquery-ui-1.8.10.custom.css"/>
<script src="scripts/jquery-1.4.4.min.js"></script>
<script src="scripts/jquery.ui.core.js"></script>
<script src="scripts/jquery.ui.widget.js"></script>
<script src="scripts/jquery.ui.position.js"></script>
<script src="scripts/jquery.ui.autocomplete.js"></script>
java代码:获取所有学校集合
public String find(){
schools = schoolManager.getSchools(schoolName);
JSONArray jsonArray = new JSONArray();
for(School s :schools){
//JSONObject obj = new JSONObject(s);
String name = s.getId().getXxmc();
jsonArray.put(name);
}
try {
printTextToClient(jsonArray.toString());
} catch (Exception e) {
log.debug("method:[find] err:"+e.getMessage());
e.printStackTrace();
}
return null;
}
public String get(){
try {
schoolName = java.net.URLDecoder.decode(schoolName,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
schools = schoolManager.findSchool(schoolName);
JSONArray jsonArray = new JSONArray();
for(School s :schools){
//JSONObject obj = new JSONObject(s);
String name = s.getId().getXxmc();
jsonArray.put(name);
}
try {
printTextToClient(jsonArray.toString());
} catch (Exception e) {
log.debug("method:[find] err:"+e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 将结果返回给xmlRequest
*
* @param s
* @param dictList
* @return
* @throws Exception
*/
public void printTextToClient(String result) throws Exception {
getResponse().setContentType("text/html;charset=gbk");
getResponse().setCharacterEncoding("gbk");
PrintWriter out = getResponse().getWriter();
try {
out.print(result);
} finally {
out.close();
}
}
这里get 和find 方法没什么差的。。。所以有点纠结
struts配置
<action name="*School" class="schoolAction" method="{1}"> <result name="success" type="redirect-action">ajaxAction</result> </action>