AJAX和Select的问题
Trouble with AJAX and Select
我正在使用AJAX调用的项目工作,在我的程序中,我有一个选择列表,我需要实现select2,但我不能这样做。我的。js文件中的代码是:
function selectAlumnos(){
$.ajax({
type : "GET",
url : lang + '../../../api/select_alumnos', //the list of data
dataType : "json",
data : {
cachehora : (new Date()).getTime()
}
}).done(function(response){
var html = '';
if(response.state == 'ok'){ //add html to the file view
html = '<select class="select_1" name="select-allalumnos" id="select-allalumnos" onchange="getIconLocation()" >'; //class to include in select2
html = html + '<option value="-1">...</option>';
for(var i in response.alumnos){
html = html + '<option value="' + response.alumnos[i].id + '" >' + response.alumnos[i].nombre + '</option>';
} //get the list of the data
html = html + '</select>'; // put the data in the list
}
$('#select-alumnos').html(html); //publish the info in the html file
});
}
在我的html页面的视图,我有这样的选择校友部分:
<label for="select-alumnos" class="select">Alumno:</label>
<span id="select-alumnos"></span> //here is the call in the AJAX
在这个文件中(html for view)我也把所有的select2路径到所需的文件,我检查了所有的文件都是ok的,我也包括了类(在我的js文件中相同的类):
$(document).ready(function() {
$(".select_1").select2();
});
</script>
我做错了什么,因为我不能得到select2格式在我的列表…?
您在.select_1
创建之前调用select2()
。在创建元素之后执行(将其放入done函数中)
.done(function(response){
var html = '';
if(response.state == 'ok'){ //add html to the file view
html = '<select class="select_1" name="select-allalumnos" id="select-allalumnos" onchange="getIconLocation()" >'; //class to include in select2
html = html + '<option value="-1">...</option>';
for(var i in response.alumnos){
html = html + '<option value="' + response.alumnos[i].id + '" >' + response.alumnos[i].nombre + '</option>';
} //get the list of the data
html = html + '</select>'; // put the data in the list
}
$('#select-alumnos').html(html); //publish the info in the html file
$(".select_1").select2();
});
相关文章:
- 在具有CONTENT标记的自定义元素中使用SELECT标记时出现问题
- Ember.Select的奇怪问题
- 寻找 Web 组件来解决这个问题(可能是 SELECT 或不知道)
- Select上通过Ajax将Javascript变量传递到PHP文件中的问题
- 使用jquery select all选项发出问题
- 使用Angular.js和PHP在select下拉列表中获取问题
- 使用jquery清除select字段,但呈现问题
- HTML & lt; select>焦点问题:使用Firefox需要2次点击来选择一个<选项>
- select .js关于无结果插件和允许默认链接行为的问题
- AJAX和Select的问题
- 在select元素上使用jQuery更改函数时出现问题
- & lt;销售:select>使用默认选项为空时,下拉问题
- 在SELECT字段中动态加载内容的问题[ANGULAR JS]
- Angular js中ng- select的问题
- AngularJS的ng-select和ng-switch-when问题
- Javascript表单验证问题(<SELECT>标记)
- 使用按钮时select中的AngularJS验证问题,但使用跨度时没有
- 关于使用jQuery连接两个Select Options val()的问题
- sqlite中select查询的循环问题
- 基于SELECT选项显示一个元素和隐藏其他元素时出现问题