如何在select2下拉列表中添加id kartik。当我选择价值时,我想要的目标"成功"在下拉列表中,因此禁用的另一个下拉列表example2和example3中的值为false。在它之前,对于dropdownlist example2和example3,其值为" yes"是假的。我已创建代码javascript但不起作用。我不知道,请告诉我。
这是我的代码:
<?php
echo $form->field($model, 'example')->widget(Select2::classname(), [
'model' => $model,
'hideSearch' => true,
'data' => ['success' => 'Success', 'fail' => "Fail"],
'language' => 'id',
'options' => [
'placeholder' => 'Pilih',
'options' => [
['id' => 'example'],
]
],
'pluginOptions' => [
'allowClear' => true,
'width' => '350px',
],
])->label('Example');
?>
<?php
echo $form->field($model, 'example2')->widget(Select2::classname(), [
'model' => $model,
'hideSearch' => true,
'data' => ['yes' => "Yes", 'no' => "No"],
'language' => 'id',
'options' => [
'placeholder' => 'Pilih',
'options' => [
'yes' => ['disabled' => true],
['id' => 'example2'],
]
],
'pluginOptions' => [
'allowClear' => true,
'width' => '350px',
],
])->label('Example 2');
?>
<?php
echo $form->field($model, 'example3')->widget(Select2::classname(), [
'model' => $model,
'hideSearch' => true,
'data' => ['yes' => "Yes", 'no' => "No"],
'language' => 'id',
'options' => [
'placeholder' => 'Pilih',
'options' => [
'yes' => ['disabled' => true],
['id' => 'example3'],
]
],
'pluginOptions' => [
'allowClear' => true,
'width' => '350px',
],
])->label('Example 3');
?>
&#13;
此javascript代码:
<?php
$this->registerJs('
$("input[type=dropdown]").change(function() {
var isi = this.value;
if(isi == "success") {
$("#example2").attr("disabled",false);
$("#example3").attr("disabled",false);
}
});
')
?>
&#13;
答案 0 :(得分:0)
可能这就是你想要的。 添加选项到第一个选择元素(#example):
'pluginEvents' => [
"change" => "function() {
var value = $(this).val();
if(value == 'success')
{
$('#demo-example2').val('yes');
$('#demo-example2').trigger('change');
$('#demo-example3').val('yes');
$('#demo-example3').trigger('change');
}
}",
]