CakePHP禁用一些单选按钮?

Eri*_*ric 6 php cakephp

我有一个带有一些单选按钮的简单表格.我想禁用一些单选按钮,这可能吗?

$sixMonths = true;
$twelveMonths = true;
$twentyfourMonths = false;

echo $this->McForm->create('Wizard',  array ('url'=>'/wizard/create'));

$options = array('24' => '24 months','12' => '12 months', '6' => '6 months');
$attributes = array('legend' =>false, 'default' => '6');
echo $this->McForm->radio('period', $options, $attributes);

echo $this->McForm->submit('Save');
echo $this->McForm->end();
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下我想禁用第一个单选按钮并启用另外两个.

我知道我可以用jQuery做到这一点,但我更愿意不使用它,它可能吗?有任何想法吗?

谢谢!

Pau*_*eus 5

您可以添加包含无线电值的disabled数组$attributes:

$attributes = [
    'legend' => false, 
    'default' => '6', 
    'disabled' => ['6', '24']
];
Run Code Online (Sandbox Code Playgroud)


Fra*_*nes 0

这是不可能的,除非您radio单独调用每个选项的函数并将您希望禁用的选项添加'disabled' => 'disabled'到数组中。$attributes这是一个可能的解决方案:

// Options
$options = array('24' => '24 months','12' => '12 months', '6' => '6 months');

// Disabled options
$disabled_options = array('12');

// Default attributes (these may need to be adjusted)
$attributes = array('legend' => false, 'default' => '6');

// Loop through all of the options
foreach ( $options as $key => $value )
{
  // Output the radio button.
  // The field name is now "period.n" which will result in "data[Model][period][n]" where "n" is the number of months.
  // The options is an array contain only the current $key and $value.
  // The 'disabled' => 'disabled' is added to the attributes if the key is found in the $disabled_options array. 
  echo $this->McForm->radio('period.' . $key, array($key => $value), ( in_array($key, $disabled_options) ? $attributes + array('disabled' => 'disabled') : $attributes ));
}
Run Code Online (Sandbox Code Playgroud)