Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择2选项不可选择_Javascript_Jquery_Select2 - Fatal编程技术网

Javascript 选择2选项不可选择

Javascript 选择2选项不可选择,javascript,jquery,select2,Javascript,Jquery,Select2,我正在使用select 2框显示一些表单选项。数据来自一个ajax请求,我将选项添加到select2。问题是当我附加选项时,我确实在下拉列表中看到它们,但无法选择它们 这是我正在使用的代码 $('#vehiclegroup-vehiclegroup_clientid').on('change', $('#vehiclegroup-vehiclegroup_clientid'),function(){ var clientType = $('#vehiclegroup-v

我正在使用select 2框显示一些表单选项。数据来自一个ajax请求,我将选项添加到select2。问题是当我附加选项时,我确实在下拉列表中看到它们,但无法选择它们

这是我正在使用的代码

    $('#vehiclegroup-vehiclegroup_clientid').on('change',
    $('#vehiclegroup-vehiclegroup_clientid'),function(){

    var clientType = $('#vehiclegroup-vehiclegroup_clienttype').val();
    var company = $('#vehiclegroup-vehiclegroup_clientid').val();
    $.ajax({

        divId: divids[1],
        url: "index.php?r=vehiclegroup/getvehiclegroups",
        data: {'company': company , 'clientType': clientType},
        method: 'GET',
        success: populateUser

    })
}); 
populateUser函数的代码

  function populateUser(data)
  {


   $(this.divId).empty();

   var newUser;
   var userList = data['list'];

   for (var client in userList )
   {
     newUser = "<option value=" + client + ">" +
        userList[client] + "</option>";

     $(this.divId).append(newUser);


   }
}
函数populateUser(数据)
{
$(this.divId).empty();
var newUser;
var userList=data['list'];
for(用户列表中的var客户端)
{
newUser=“”+
用户列表[客户端]+“”;
$(this.divId).append(newUser);
}
}
我在同一页上的另一个select2框中使用相同的功能,它工作正常

编辑:

我的Select 2初始化代码如下

<?= $form->field($model, 'vehiclegroup_parentid')->widget(Select2::classname(),
        [
            'data' => [],
            'language' => 'en',
            'options' => ['placeholder' => 'Select SubGroup Name'],
            'pluginOptions' => ['allowClear' => true],
        ]
    )->label('SubGroup'); ?>

确保选择2初始化

<?= $form->field($model, 'vehiclegroup_parentid')->widget(Select2::classname(),
    [
        'data' => [],
        'language' => 'en',
        'options' => ['placeholder' => 'Select SubGroup Name'],
        'pluginOptions' => ['allowClear' => true],
    ]
)->label('SubGroup'); ?>


是在ajax请求完成之后,而不是在此之前。您的问题是,在将选项附加到select2之前,您的select2已初始化,因此select2将无法识别以后添加的选项。

我解决了此问题。我无法选择第一个选项,首先我需要选择任何其他选项,然后它变为可选择。我搜索了一下,这是因为即使在附加了选项之后,也需要触发更改

   $(this.divId).select2().trigger('change');

你在哪里初始化。select2()插件,你能在这里发布同样的内容吗。我在视图中使用的是Yii widget类。好的,快速检查,检查你是否在初始化select2之后初始化select2。如果你在初始化select2之后没有ajax,那么ajax请求就是在初始化select2之后。