【问题标题】:Add class to the label for radio button in cakephp 3在 cakephp 3 中为单选按钮的标签添加类
【发布时间】:2017-02-13 15:30:18
【问题描述】:

我在 cakephp 3 中向单选按钮标签添加类时遇到问题。

我的代码是:

<?= $this->Form->input('enroll',[
    'type'=>'radio',
    'options'=>[
        [
            'value'=>'1',
            'text'=>'Yes',
            'class'=>'checkbox'
        ],
        [
            'value'=>'0',
            'text'=>'No',
            'class'=>'checkbox'
        ]
    ],
    'label'=>false
  ])
?>

这是生成以下 Html:

<div class="input radio">
    <input type="hidden" value="" name="enroll">
    <label for="enroll-1">
        <input type="radio" id="enroll-1" class="checkbox" value="1" name="enroll">
        Yes
    </label>
    <label for="enroll-0">
        <input type="radio" id="enroll-0" class="checkbox" value="0" name="enroll">
        No
    </label>
</div>

我想要的是:

<div class="input radio">
<input type="hidden" value="" name="enroll">
<label for="enroll-1" class="checked-input">
    <input type="radio" id="enroll-1" class="checkbox" value="1" name="enroll">
    Yes
</label>
<label for="enroll-0" class="checked-input">
    <input type="radio" id="enroll-0" class="checkbox" value="0" name="enroll">
    No
</label>

我想将一个类添加到&lt;label for=''&gt;

我尝试的是:

<?= $this->Form->input('enroll',[
    'type'=>'radio',
    'templates'=>[
        'label'=>'<label {{attrs}} class="checked-input">{{text}}</label>',
        'radioWrapper'=>'{{label}}'
    'options'=>[
        [
            'value'=>'1',
            'text'=>'Yes',
            'class'=>'checkbox'
        ],
        [
            'value'=>'0',
            'text'=>'No',
            'class'=>'checkbox'
        ]
    ],
    'label'=>false
  ])
?>

但我无法做到这一点。我已经尝试了很多其他的东西,但它们也不起作用。请帮忙。

【问题讨论】:

  • {$this-&gt;Form-&gt;radio('enroll', $options, ['label' =&gt; ['class' =&gt; 'checked-input']])} 成功了

标签: php cakephp cakephp-3.x


【解决方案1】:

只需修改nestingLabel 模板而不是label 模板

'nestingLabel' => '{{hidden}}<label{{attrs}} class="checked-input">{{input}}{{text}}</label>'

【讨论】:

  • 非常感谢先生。 @arilia
  • echo $this->Form->input('reason', array('options' => $reasons, 'type' => 'radio', 'label' => false, '必需' => true, 'templates' => [ 'nestingLabel' => '{{hidden}}', ] ));不工作,我知道为什么吗?
【解决方案2】:

这也有效。

<?= $this->Form->input('current_password', [
                'type' => 'password',
                'required' => true,
                'label' => ['class' => 'hello']]);
        ?>

【讨论】:

    猜你喜欢
    • 2013-04-21
    • 2012-04-30
    • 2016-01-13
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多