CakePHP禁用一些单选按钮?

时间:2011-06-28 05:17:16

标签: 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();

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

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

谢谢!

2 个答案:

答案 0 :(得分:5)

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

$attributes = [
    'legend' => false, 
    'default' => '6', 
    'disabled' => ['6', '24']
];

答案 1 :(得分: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 ));
}