1

如何在 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>
4

1 回答 1

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))

这是主要思想。希望这可以帮助

于 2012-06-04T17:16:55.787 回答