4

Setting Select to to 'disabled' does not disable the element. The user can still click on the text of Select2 and the options box opens up. Here is a disabled control that was opened by clicking on the text and not the down-arrow button.

Disabled Control:

Here is my code:

<?= $form->field($model, 'billing_currency_id')->widget(Select2::className(), [
'data' => BillingCurrency::listIdCodes('','',true),
'disabled' => true,
'options' => ['disabled' => true,],
'pluginOptions'=>[
    'allowClear'=>false,
    'dropdownAutoWidth'=>true,
    'disabled' => true,
], ]); ?>

Clicking on the down-arrow button keeps the control closed, but clicking on the text area of the control opens the options box.

UPDATE Found my own mistake - see answer below.

4

2 回答 2

3

我犯了一个错误 - 我在网站上有一些自定义 JS 代码,当它获得焦点时会打开 Select2。我的代码导致了报告的问题。
创建我的自定义代码是为了克服 Select2 的限制,即当用户进入控件时它不会自动打开。我已经更正了代码。当 Select2 从选项卡或单击获得焦点时,控件将打开弹出窗口,除非它被禁用。(以前我没有检查 disabled 属性。)

$(document).on('focus', '.select2', function() {
    var elSelect = $(this).siblings('select');
    if (elSelect.is('[disabled]')==false) {
        elSelect.select2('open');
    }
});
于 2016-01-21T04:08:35.653 回答
1

禁用选择在示例(禁用模式)部分的官方文档中进行了描述:

Select2 将响应<select>元素上的 disabled 属性。您还可以使用 disabled: true 初始化 Select2 以获得相同的效果。

至于 Kartik 的扩展,你可以'disabled' => true在下面options这样设置:

'options' => ['disabled' => true],

我检查只是为了确定,它有效。

于 2016-01-21T03:21:49.067 回答