【问题标题】:Getting always last value when trying to create a radio button using loop in cakephp尝试使用 cakephp 中的循环创建单选按钮时始终获取最后一个值
【发布时间】:2022-01-03 13:18:26
【问题描述】:

我正在尝试使用循环创建单选按钮

<?= $this->Form->create($account) ?>
<?php foreach($gametitles as $gametitle): ?>
        <?= $this->Form->radio(
                  'gametitle_id',
                         [
                           ['value' => $gametitle->id,'text'=>'','hiddenField' => false],
                         ]
             ); 
        ?>
<?php endforeach; ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

我添加了 'hiddenField' =&gt; false ,但在 DOM 中我看到仍然显示隐藏字段。如果我在没有最后一个单选按钮的情况下选择,在输出中我总是得到空值。

[
'gametitle_id' => '',
]

如果我使用名称作为数组 gametitle_id[] 我得到一个数组

'gametitle_id' => [
(int) 0 => '',
(int) 1 => '',
(int) 2 => '4',
(int) 3 => '',
(int) 4 => '',
],

如何只获得一个在单选按钮中选择的值?我用了'hiddenField' =&gt; false,为什么还显示隐藏字段?

【问题讨论】:

    标签: cakephp cakephp-4.x


    【解决方案1】:

    您在选项中设置了'hiddenField' =&gt; false!你必须用名字来设置它。

    echo $this->Form->radio(
        'favorite_color',
        [
            ['value' => 'r', 'text' => 'Red', 'style' => 'color:red;']
        ],
        ['hiddenField' => false]
    )
    

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多