之前用本地下拉框数据 搜索框正常使用 ajax就不行了
js代码
/* 执行select2方法初始化 */
function select2Execute() {
$(function() {
/*1-本地注入方式*/
/* $(".downList2").select2({
// $("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',
});*/
//2- AJAX获取数据方式
$(".downList2").select2({
ajax: {
url: 'http://localhost:8080/erzhentang/manage/forAjax.do?requestType=12.5_1',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term,
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
minimumInputLength: 0,
formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
});
select标签 拼接在js里了
var str = "";
str +="<select name=\"xuewei_id\" class=\"form-control input-sm downList2\" multiple>";
str +="<option></option>";
"</select>";
你在浏览器里请求 http://localhost:8080/erzhentang/manage/forAjax.do?requestType=12.5_1 返回的是什么
之前让你抓包,你抓了么?把结果贴出来啊。
processResults: function (data) {
return {
results: data
};
这个里面data对应的json是不是列表
看下forAjax.do是不是只接收post请求,如果那样,加上method:"post"
如果服务器没有返回或者返回不对,你在服务器上直接返回你之前静态的json试试看
而且 选中的项目 无法 回显
楼主好 不知道你问题解决没有 我不是很擅长这个前端 我有个解决方案 你可以参考一下 你说本地数据可以使用 你是否可以先用ajax请求数据 然后在ajax回调中在渲染select2
这种方式不就相当于用本地数据了 这样应该是可以的吧 你要不试试先
你说的回显是提交到后台回到这个页面时默认选中?如果是的话,你页面是否还有个选中的东西?
谷歌浏览器,有开发者工具,可以使用开发者工具查看,火狐也有,主要是看看具体问题