如何选择多选下拉选项?

时间:2018-09-10 12:01:36

标签: php json ajax codeigniter-2 multi-select

我必须多次选择下拉选项 而且我已经在ajax中获取了结果

$.ajax({
    type    : 'POST',
    data: {id:id},
    url     : url,
    dataType  : 'json',
    success: function(data){
        var len = data.length;
        for (var i = 0; i < len; i++) {
            var templatename1 = data[i]['templatename'];
            var fields = data[i]['fields'];
            $('#selectColumn1').val(fields);
        } 
    }
});

var fields = data[i]['fields'];中的响应就像a,b,c,d(逗号分隔)

那么如何在下拉菜单中选择保留的记录复选框,像a,b,c这样选择呢?

1 个答案:

答案 0 :(得分:1)

 $.ajax({
    type    : 'POST',
    data: {id:id},
    url     : url,
    dataType  : 'json',
    success: function(data){
        var len = data.length;
        for (var i = 0; i < len; i++) {
            var templatename1 = data[i]['templatename'];
              if(data[i]['fields']){ 
                 var fields = data[i]['fields'].split(","); // you have to convert comma seperated string into array
              }
              $('#selectColumn1').val(fields);
              $("#selectColumn1").multiselect("refresh"); // To refresh multiselect
        }
    }
});