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

时间:2015-04-06 03:23:32

标签: yii

所以我有2 $form->dropDownList

如果选择了其中一个下拉列表,另一个下拉列表设置为值null并禁用,则如何以某种方式创建它,反之亦然。

我可以在array()中添加哪些选项,以便它按照我想要的方式运行?

请指教。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你需要通过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");
   });
});