CakePHP отключить некоторые радиокнопки? - PullRequest
6 голосов
/ 28 июня 2011

У меня есть простая форма с некоторыми переключателями.Я хочу отключить некоторые переключатели, возможно ли это?

$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();

Так что в этом случае я бы хотел отключить первый переключатель и включить два других.

Iзнаю, что я мог бы сделать это с помощью jQuery, но я бы предпочел сделать это без его использования, это возможно?Есть идеи?

Спасибо!

Ответы [ 2 ]

5 голосов
/ 23 января 2014

Вы можете добавить массив disabled в $attributes, который содержит значения радиостанций:

$attributes = [
    'legend' => false, 
    'default' => '6', 
    'disabled' => ['6', '24']
];
0 голосов
/ 28 июня 2011

Это невозможно, если вы не вызовете функцию 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 ));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...