如何在cake php中为select标签赋予一个属性?

时间:2012-06-04 12:48:27

标签: cakephp cakephp-1.3

如何在cake php中为select标签赋一个属性?

$options = array('0' => 'News', '1' => 'Movies');
    echo $this->Form->select('selectValue', $options, 0, array('id' => 'select') )

这样我就可以为每个选项提供属性data-url

<select id="select" name="data[Video][gender]" >
  <option value="0" data-url = "/news" >News</option>
  <option value="1" data-url = "/movies" >Movies</option>
</select>

1 个答案:

答案 0 :(得分:0)

你不能:P或者至少没有简单的方法可以做到这一点......

您需要扩展FormHelper并覆盖select()方法。

类似的东西:

class MyFormHelper extends FormHelper{
    public function select($fieldName, $options = array(), $attributes = array()) {
        //something like the original method but this time the $options 
        //is more than a simple key=>value array, so you'll need
        //to change the code to include the data-url
    }
}

然后使用MyFormHelper调用你的助手,比如

//In the view
$options = array(
                 array('data-url'=>'/news','text'=>'News'),
                 array('data-url'=>'/movies','text'=>'Movies'),
);

$this->MyForm->select('field',array('options'=>$options))

这是主要想法。希望这有帮助