一個 簡單的 自動補全例子:
js代碼:
$('#username').autocomplete({ source: function(request, response) { $.ajax({ url:'userList', type: 'POST', data: {"key":$('#username').val()}, dataType: 'json', success: function(data){ response(data); }, error: function(error){ alert('數據處理異常,請聯繫相關人員或刷新重試'); } }); }, minChars: 1, //最小顯示條數 max: 3 //最大顯示條數 });
html:
<input type="text" id="username"/>
action:
/**
* @author myemptyname
* @date 2012-09-27
*/
public class UserAction extends ActionSupport {
private List<String> userNameList;
private String key;
private UserService serivce;
public String execute(){
return SUCCESS;
}
public String userList(){
this.userNameList = this.service.getAllUserName(this.key);
return "userList";
}
public List<String> getUserNameList() {
return userNameList;
}
public void setUserNameList(List<String> userNameList) {
this.userNameList = userNameList;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
struts.xml
<action name="user_*" class="UserAction " method = "{1}"> <result name="success" type="json"></result> <result name="userList" type="json"> <param name="root">userNameList</param> <!-- 限定傳過去json參數,我們只需要列表 --> </result> </action>