Yii - 选择另一个时禁用下拉

Yii - disabling drop down when another is selected

所以我有 2 $form->dropDownList

如果选择了其中一个下拉菜单,另一个下拉菜单将设置为值 null 并被禁用,反之亦然。

我可以将哪些选项添加到 array() 中以使其按照我想要的方式运行?

请指教。提前致谢。

您需要通过javascript或jquery进行处理。首先,您应该以这种方式为两个下拉菜单定义一个 ID:

<?php echo $form->dropDownList($yourModel, 'attribute', CHtml::listData(...), array("id" => 'dropDown1')); ?>

<?php echo $form->dropDownList($yourModel, 'attribute', CHtml::listData(...), array("id" => 'dropDown2')); ?>

并且在脚本中:

$(document).ready(function(){
   if($("#dropDown1").val())
      $("#dropDown2").attr("disabled", "disabled");
   else
      $("#dropDown2").removeAttr("disabled");

   if($("#dropDown2").val())
      $("#dropDown1").attr("disabled", "disabled");
    else
      $("#dropDown1").removeAttr("disabled");

   $($("#dropDown1").on("change", function(){
       if($(this).val())
          $("#dropDown2").attr("disabled", "disabled");  
       else
          $("#dropDown2").removeAttr("disabled");
   });
   $($("#dropDown2").on("change", function(){
       if($(this).val())
          $("#dropDown1").attr("disabled", "disabled");  
       else
          $("#dropDown1").removeAttr("disabled");
   });
});