2013-03-10 63 views
3

我正在使用Yii 1.1.10。我想知道如何將CSS類添加到下拉列表中。我使用CActiveFormYii:如何將CSS類添加到下拉列表

例如,我將如何添加一個CSS類到這個下拉列表?

<?php echo $form->labelEx($model,'chassis'); ?> 
<?php echo $form->dropDownList($model, 'chassis', 
     array('saloon' => 'saloon', 'station wagon' => 'station wagon',), 
    ); 
?> 

編輯: 我有這個在我的代碼

array('empty' => 'Select one of the following...') 

我有它存在,使其默認的消息。但不知何故,干擾使用

'htmlOptions'=>array('class'=>'yourCssClass') 

OR

array('class'=>'your_class_name') 

所以只要我刪除它,兩項建議的工作!謝謝你們

+0

P.S要使用佔位符使用'prompt'而不是'empty' – topher 2013-03-10 21:08:50

回答

3

您可以使用CActiveFrom::dropDownList()htmlOptions參數指定class,style或任何其他HTML屬性。

<?php echo $form->labelEx($model,'chassis'); ?> 
<?php echo $form->dropDownList($model, 'chassis', 
     array('saloon' => 'saloon', 'station wagon' => 'station wagon',), 
     array('class'=>'your_class_name'), 
    ); 
?> 
+0

我已經嘗試過,但它不工作;我使用了chrome並檢查了下拉菜單並沒有上課......你有什麼建議.. – 2013-03-10 19:50:05

+0

你在選擇標籤中獲得了一個班級。你想要每個選項標籤上的課程嗎? – dInGd0nG 2013-03-10 19:52:45

+0

好吧,它的工作原理。這是因爲我有工作。額外補充說,我遺漏在我的問題... ive做了一個編輯我的問題,所以你可以看到它.. @ naselorm samething,你的建議作品太謝謝你們.. – 2013-03-10 20:06:13

1

這是根據Yiiframework API爲DROPDOWNLIST語法

public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ()) 

最後一部分,您可以通過任何HTML值,你通常會在做一個HTML page.Thus旅行車後添加此,

'htmlOptions'=>array('class'=>'yourCssClass'), 
0

顯示數組值並存儲數組鍵。

這是型號代碼。

public function getCityOptions() 
{ 
    $list=array('saloon' => 'saloon', 'station wagon' => 'station wagon',); 
    asort($list); 
    return $list; 
} 

這_form.php這個代碼

<div class="row"> 
<?php echo $form->labelEx($model,'chassis'); ?> 
<?php echo $form->dropDownList($model,'chassis',$model->getCityOptions()); ?> 
<?php echo $form->error($model,'chassis'); ?> 
</div> 

試試這個..

0

這是一個答案。 :)

<?php echo $form->labelEx($model,'chassis'); ?> 
<?php echo $form->dropDownList($model, 'chassis', 
    array('saloon' => 'saloon', 'station wagon' => 'station wagon',), 
    array('class'=>'form-control','empty' => 'Select one of the following...') 
); 
?>