【问题标题】:Cakephp Form Radio Button Label ClassCakephp 表单单选按钮标签类
【发布时间】:2012-04-30 15:13:06
【问题描述】:

我想通过表单助手来使用单选按钮。单选按钮有一个 Radio 元素和一个 Label。默认情况下,我有显示:标签元素的块。我想为一个类分配单选按钮的标签,以便我可以为其分配一个内联块。

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

如何将类分配给选项的标签

【问题讨论】:

    标签: cakephp radio-button label cakephp-2.0


    【解决方案1】:

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

    但是要修改这些标签的显示,你可以使用包围div

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

    并像这样使用 CSS:

    .inline_labels label
    {
        display: inline-block;
    }
    

    【讨论】:

    • 是的,标签现在很好地放在一行上。唯一不好的是,他们失去了与单选按钮的连接。
    【解决方案2】:

    如何使用FormHelper::label(string $fieldName, string $text, array $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

    【讨论】:

      【解决方案3】:

      为我工作:

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

      享受

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 2011-07-27
        • 2013-03-18
        • 1970-01-01
        • 2011-11-10
        • 1970-01-01
        相关资源
        最近更新 更多