首页 > 代码库 > jQuery ajax 获取信息展示在“下拉列表”中

jQuery ajax 获取信息展示在“下拉列表”中

<link href="http://www.mamicode.com/${ctxStatic}/jquery-select2/4.0.3/select2.min.css" rel="stylesheet" />
<script src="http://www.mamicode.com/${ctxStatic}/jquery-select2/4.0.3/select2.full.min.js" type="text/javascript"></script>
<script src="http://www.mamicode.com/${ctxStatic}/jquery-select2/4.0.3/i18n/zh-CN.js" type="text/javascript"></script>


$("[data-toggle=‘tooltip‘]").tooltip({html: true});
//获取整个列表
$(‘#primaryAuditUserId‘).select2({
language: "zh-CN",
minimumInputLength: 1,
placeholder: "-- 请输入昵称搜索 --",
ajax: {
url: "xxx/xxx/xxxx/userAll",
dataType: "json",
delay: 500,
data: function (params) {
return {name: params.term};//name为入参标识
},
processResults: function (res, params) {
var options = [];
console.log("res:"+res);
$(res).each(function (index, r) {
var option = {"id": r.id, "text": r.name};//列表中的id,列表中的name
options.push(option);
});
return {
results: options
};
},
escapeMarkup: function (m) {
return m;
}
}
});

//获取ID
(function () {
var id = $("#select2-userId").val();
if (id) {
$.get("xxxxxx/xxxx/single/" + id, function (result) {
if (result) {
$("#primaryAuditUserId").html("<option value=http://www.mamicode.com/‘" + result.id + "‘>" + result.name + "</option>");//
}
});
}
})();



<li>
<label>初审人:</label>
<input type="hidden" id="select2-userId" value="http://www.mamicode.com/${infoDto.primaryAuditUserId}"/>
<form:select path="primaryAuditUserId" class="input-medium"></form:select>
</li>

 

接口:

//模糊匹配列表
@RequestMapping(value="http://www.mamicode.com/userAll")
@ResponseBody
public List<User> getUsers(String name){
User user=new User();
user.setDelFlag("0");
user.setName(name);
List<User> userList=userDao.findList(user);
if(userList.size()<=0||userList==null){
return null;
}
return userList;
}

//查询单个的id
@RequestMapping(value="http://www.mamicode.com/singUser")
@ResponseBody
public String getSingleUser(String name){
User user=new User();
user.setName(name);
User user_=userDao.getSingle(user);
return user_.getId();
}

jQuery ajax 获取信息展示在“下拉列表”中