Cakephp表单单选按钮标签类

时间:2012-04-19 05:59:13

标签: cakephp radio-button label cakephp-2.0

我想使用Form Helper来使用单选按钮。单选按钮具有Radio元素和Label。我默认显示:标签元素的块。我想为一个类分配单选按钮的标签,以便我可以为它分配一个内联块。

$attributes  = array('legend' => false, 'label' => array('class' => 'radioBtn'));
echo $this->Form->radio('gender', $options, $attributes);

如何将类分配给选项

的标签

3 个答案:

答案 0 :(得分:5)

通过查看Form-> radio()方法代码,似乎与属于标签的属性无关。

但要修改这些标签的显示,您可以使用周围的div

echo '<div class="inline_labels">';
echo $this->Form->radio('gender', $options, $attributes);
echo '</div>';

并使用这样的CSS:

.inline_labels label
{
    display: inline-block;
}

答案 1 :(得分:1)

如何使用FormHelper::label(string $fieldName, string $text, array $options) 您可以在options数组中定义标签类,因此(例如):

echo $options = array( /* relevant stuff goes here */ );
echo $attributes = array( /* relevant stuff goes here */ );
echo $this->Form->radio('gender', $options, $attributes);
echo $this->Form->label('gender', 'Text of your label', array('label'=>'radioBtn'))

来源CakePHP Cookbook on FormHelper

答案 2 :(得分:1)

为我工作:

//  Add your own label with CSS class
$opts = array('1' => "<label class='myCSS'>My first option</label>", "2" => "<label class='myCSS'>My second option</label>");

//  Put label param to false
echo $this->Form->input('my-input', array('type' => 'radio', 'label' => false, 'options' => $opts, 'legend' => false));

享受