下拉框没有任何选项内容
js代码
/* 执行select2方法初始化 */
function select2Execute() {
var dataList = [
{ id: 0, text: 'enhancement' },
{ id: 1, text: 'bug' },
{ id: 2, text: 'duplicate' },
{ id: 3, text: 'invalid' },
{ id: 4, text: 'wontfix' }
];
var dataList1=[{'id':"1","text":"enhancement"}];
$(function() {
/*本地注入方式*/
/*$("select[name='xuewei_id']").select2({
// $('select[id="xuewei_id"]').select2({
// $('#xuewei_id').select2({
// $("#selectSection").find("select.combox").select2({
// $(".select2-selection__choice").select2();
// $(".combox").select2({
placeholder : "请至少选择一个人名",
tags : true,
createTag : function(decorated, params) {
return null;
},
width : '256px',
});*/
//two AJAX获取数据方式
//$("select[name='xuewei_id']").select2({
$(".downList2").select2({
ajax: {
type:'GET',
// url: '/erzhentang/manage/forAjax.do?requestType=12.1',
url: dataList1,
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term 请求参数
page: params.page
};
},
processResults: function (data, params) {
params.page = params.page || 1;
/*var itemList = [];
var arr = data.result.list
for(item in arr){
itemList.push({id: item, text: arr[item]})
}*/
return {
results: data.items,//itemList
pagination: {
more: (params.page * 2) < data.total_count
}
};
},
cache: true
},
placeholder:'请选择',//默认文字提示
language: "zh-CN",
tags: true,//允许手动添加
allowClear: true,//允许清空
escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
minimumInputLength: 0,
formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
});
function formatState(state) {
if (!state.id) {
return state.text;
}
var $state = $('<span>' + state.text + '</span>');
return $state;
}
;
$('#sel_recommender').select2({
placeholder : "请选择一个人名",
templateResult : formatState,
width : '256px'
});
// 通过id获取select2的text值,这里的text值可能有空格,需注意
function getSelect2Text(obj) {
var select2Obj = $("select[name='xuewei_id']").select2();
return select2Obj.find("option:selected").text();
}
/* 获取每一个name对应的值 */
var strXueweiId = "";
$("[name=xuewei_id]").each(function() {
alert("this值:" + $(this).val());
/* 拼接每一项name的值 组合成和其它项目类似 gender那样的数组 */
strXueweiId += $(this).val() + "-";
});
alert("strXueweiId:" + strXueweiId)
$("#strXueweiId").val(strXueweiId)
});
}
看你的java程序,processResults:返回的对不对
返回的选项必须处理成以下格式
var results = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
https://blog.csdn.net/szhielelp/article/details/73556066
//two AJAX获取数据方式
这个方法里的url是dataList1,这个是个json,是不是你的参数啊
好像你没写对url
后台代码
if("12.5_1".equals(requestType)){//查询科研检测穴位
String jsonOM = "["+ "{\"id\":\"68\",\"body_name\":\"平和质\"},{\"id\":\"104\",\"body_name\":\"特禀质\"}]";
JSONArray ja = JSONArray.fromObject(jsonOM);
OutputUtil.outPutJsonArrary(response, ja);// 用jsonArray数组
}
js代码 url已更换 还是 不执行
$(".downList2").select2({
ajax: {
type:'GET',
url: '/erzhentang/manage/forAjax.do?requestType=12.5_1',
// url: dataList1,
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term 请求参数
page: params.page
};
},