问题
How to give select tag an attribute in cake php ?
$options = array('0' => 'News', '1' => 'Movies');
echo $this->Form->select('selectValue', $options, 0, array('id' => 'select') )
So that I can give a attribute data-url
to every options
<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:
you can't :P or at least there's no easy way to do it...
You'll need to extend the FormHelper and override the select()
method.
Something like:
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
}
}
and then call your helper using MyFormHelper, something like
//In the view
$options = array(
array('data-url'=>'/news','text'=>'News'),
array('data-url'=>'/movies','text'=>'Movies'),
);
$this->MyForm->select('field',array('options'=>$options))
That's the main idea. Hope this helps
来源:https://stackoverflow.com/questions/10881586/how-to-give-select-tag-an-attribute-in-cake-php