如何在Forms API中默认检查复选框


18

我想知道如何才能默认选中一个复选框吗?是#default_value错误的属性吗?

 $form['ios'] = array(
            '#title' => t(''),
            '#type' => 'checkboxes',
            '#description' => t(''),
            '#options' => $options,
            '#default_value' => 'checked' // this is not working

        ); 

非常感谢!

Answers:


23

请参阅以下示例...

$options = array();
$options["1"] = "One";
$options["2"] = "Two";
$options["3"] = "Three";
$options["4"] = "Four";


$form['ios'] = array(
  '#title' => t(''),
  '#type' => 'checkboxes',
  '#description' => t(''),
  '#options' => $options,
  '#default_value' => array("1", "2", "3")
); 

17

您具有“新闻信”或“条款条件”之类的“单一字段”复选框,可以使用以下代码

 $form['name']['terms_condition'] = array(
      '#type' =>'checkbox',
      '#title'=>t('Terms and conditions'),
      '#required'=>TRUE,
      '#default_value' =>TRUE, // for default checked and false is not checked
  );

1
如果在未选中复选框但要求将其设置为的情况下提交表单,会发生什么情况true
跨越

1

你尝试了吗?

    $form['ios'] = array(
        '#title' => t(''),
        '#type' => 'checkboxes',
        '#description' => t(''),
        '#options' => $options,
        '#default_value' => array($value) // this is not working

    ); 
    //$value should be the option you want to have 

奥斯卡

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.