简体   繁体   English

在 Cakephp 中设置默认单选按钮

[英]Set default radio button in Cakephp

The below code creates some radio buttons and html to go with them, it works well however I now want to set the first radio button as the default selected one and im not sure what needs to be added.下面的代码用它们创建了一些单选按钮和 html 到 go,效果很好,但是我现在想将第一个单选按钮设置为默认选择的单选按钮,我不确定需要添加什么。

<?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>";
          }

?>

I'm not really familiar with CakePHP but a quick Google Search gave me this 我对CakePHP并不是很熟悉,但是快速的Google搜索给了我这个

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

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

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

So you should add the attribute 'value' and set it as your default selected radio 因此,您应该添加属性“值”并将其设置为默认选择的单选

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

If you try the associated value option it should work. 如果尝试使用关联的值选项,则它应该可以工作。

<?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'
) ); ?>

Try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM