本文介绍了如何给选择标签一个属性在蛋糕php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CakePHP中给选择标签属性?

  $ options = array('0'=>新闻','1'=>'电影'); 
echo $ this-> Form-> select('selectValue',$ options,0,array('id'=>'select'))
/ pre>

这样我可以给每个选项一个 data-url 属性

 < select id =selectname =data [Video] [gender]> 
< option value =0data-url =/ news>新闻< / option>
< option value =1data-url =/ movies> Movies< / option>
< / select>


解决方案

您不能:P或至少没有...



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



类似:

  class MyFormHelper extends FormHelper {
public function select($ fieldName,$ options = array(),$ attributes = array()){
//像原来的方法,但这次$ options
//比简单的key =>值数组,所以你需要
//来改变代码以包含data-url
}
}

,然后使用MyFormHelper调用您的助手,例如

  //在视图
$ options = array(
array('data-url'=>'/ news','text'=>'News'),
array('data-url'=>'/ movies','text'=>'Movies'),
);

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

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


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>
解决方案

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

这篇关于如何给选择标签一个属性在蛋糕php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:14