CakePHP 2.1创建选择框

时间:2012-04-28 15:07:44

标签: cakephp drop-down-menu cakephp-2.1

我正在尝试添加以下样式..

<div class="rowElem noborder">
        <label>Language:</label>
        <div class="formRight noSearch">
          <select name="select2" class="chzn-select">
            <option value="opt1">Choose the Language</option>
            <option value="opt2" selected="selected">Kannada</option>
            <option value="opt3">Telugu</option>
            <option value="opt4">Tamil</option>
          </select>
        </div>
        <div class="fix"></div>
</div>

但在cakephp中,我有这段代码

<?php echo $this->Form->input('language_id', array('class' => 'chzn-select' )); ?>

请给我解决方案..

3 个答案:

答案 0 :(得分:1)

如果我理解你的要求,这就是你需要做的。

在控制器中,您将为选择框创建选项数组:

$this->set('languageOptions', array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil'));

然后在视图中,您创建表单:

<div class="rowElem noborder">
    <label for="language_id">Language:</label>
    <?php echo $this->Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
    <div class="fix"></div>
</div>

答案 1 :(得分:0)

$langs = array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil');

$this->set(compact('langs')); // if you set options from controller

然后在视图中试试这个:

$this->Form->input('language_id', array(
                                      'type' => 'select',
                                      'options' => $langs,
                                      'selected' => 2 // suppose default select Kannada
                                  )
                  );

答案 2 :(得分:-2)

Cake Php选择选项代码

    语言:     表格 - >输入(&#39; language_id&#39;,数组(&#39;类&#39; =&gt;&#39; chzn-select&#39;,&#39;选项&#39; =&gt; $ languageOptions,&#39; label&#39; =&gt; false,&#39; div&#39; =&gt;数组(&#39; class&#39; =&gt;&#39; formRight noSearch&#39;) )); ?&GT;