在Cakephp中设置默认单选按钮

pn9klfpd  于 2022-11-11  发布在  PHP
关注(0)|答案(4)|浏览(192)

下面的代码创建了一些单选按钮和html来配合它们,它工作得很好,但是我现在想把第一个单选按钮设置为默认的选中按钮,我不确定需要添加什么。

<?php
     for ($i =0; $i < count($packages); $i++){
       echo "<div class='package-outter'>";
       echo "Name: ".$packages[$i]['Package']['name']."<br>";
       echo "Number of Campaigns (per month): ".$packages[$i]['Package']['quantity']."<br>";
       echo "Price: ".$packages[$i]['Package']['price']."<br>";

         if ($i == 0){
           echo $this->Form->input('package', array(
                                   'type' => 'radio',
                                   'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
                                   'class' => 'testClass',
                                    ));
         }else{
           echo $this->Form->input('package', array(
                                   'type' => 'radio',
                                   'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
                                   'class' => 'testClass',
                                   'hiddenField' => false, // added for non-first elements
                                    ));
          }

          echo "</div>";
          }

?>
ejk8hzay

ejk8hzay1#

我不是很熟悉CakePHP,但是快速的Google搜索给了我这个

$options = array(
    'standard' => 'Standard',
    'pro' => 'Pro'
);

$attributes = array(
    'legend' => false,
    'value' => $foo
);

echo $this->Form->radio('type', $options, $attributes);

因此,您应该添加属性“value”,并将其设置为默认选定的单选按钮

rpppsulh

rpppsulh2#

如果您尝试使用相关的值选项,它应该可以正常工作。

<?php 
echo $this->Form->input('status', array(
    'div' => true,
    'label' => true,
    'type' => 'radio',
    'legend' => false,
    'required' => false,
    'hiddenField'=>false,
    'options' => array(
            1 => 'High',
            2 => 'Medium', 
            3 => 'Low'
        )
    'value' => 'Medium'
) ); ?>
balp4ylt

balp4ylt3#

<?php 
echo $this->Form->input('status', array(
    'div' => true,
    'label' => true,
    'type' => 'radio',
    'legend' => false,
    'required' => false,
    'hiddenField'=>false,
    'options' => $status,
    'value' => 1 => default value of input
   ));
huwehgph

huwehgph4#

试试这个

$this->Form->control('company_id', [
 'label' => 'Escolha a empresa', 
 'options' => $companies , 
 'value'=> key($companies), 
 'type' => 'radio', 
 'required' => true
])

相关问题