Drupal 表单 api 复选框

发布于 2024-10-16 15:20:43 字数 1220 浏览 1 评论 0原文

我正在使用 drupal 表单 api 并使用复选框。我在默认检查值中遇到问题。以下是代码片段...

$result = db_query("SELECT nid, filepath FROM {content_type_brand}, {files} WHERE content_type_brand.field_brand_image_fid
= files.fid");
        $items = array();
        while ($r = db_fetch_array($result)) {
                array_push($items, $r);
        }
        $options = array();
        foreach( $items as $i ) {
            $imagePath = base_path().$i['filepath'];
            $options[$i['nid']] = '<img src="'.$imagePath.'"></img>';
        }
        $form['favorite_brands'] = array (
            '#type' => 'fieldset',
            '#title' => t('Favorite Brands'),
            //'#weight' => 5,
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
        );
         $form['favorite_brands']['brands_options']
= array(
            '#type' => 'checkboxes',
            '#options' => $options,
            '#default_value' => $options_checked,// $options_checked is an array similar to $options but having elements which need to be checked by default...
            '#multicolumn' => array('width' => 3)
        );

但默认情况下不检查值...任何人都可以帮助我缺少什么吗?

谢谢

I am using drupal form api and using checkboxes. I am getting problem in default checked values with it. following is the code snippet...

$result = db_query("SELECT nid, filepath FROM {content_type_brand}, {files} WHERE content_type_brand.field_brand_image_fid
= files.fid");
        $items = array();
        while ($r = db_fetch_array($result)) {
                array_push($items, $r);
        }
        $options = array();
        foreach( $items as $i ) {
            $imagePath = base_path().$i['filepath'];
            $options[$i['nid']] = '<img src="'.$imagePath.'"></img>';
        }
        $form['favorite_brands'] = array (
            '#type' => 'fieldset',
            '#title' => t('Favorite Brands'),
            //'#weight' => 5,
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
        );
         $form['favorite_brands']['brands_options']
= array(
            '#type' => 'checkboxes',
            '#options' => $options,
            '#default_value' => $options_checked,// $options_checked is an array similar to $options but having elements which need to be checked by default...
            '#multicolumn' => array('width' => 3)
        );

but values are not checked by default... can anyone help what I am missing??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2024-10-23 15:20:43

您的 $options_checked 数组不应与 $options 数组采用相同的格式。您的 $options 数组包含 nid => img 标签对。您的 $options_checked 数组应该只包含默认情况下应检查的选项的 nid 值:

$options_checked = array(8,17);

Your $options_checked array should not be in the same format as your $options array. Your $options array contains nid => img tag pairs. Your $options_checked array should simply contain the nid values of the options that should be checked by default:

$options_checked = array(8,17);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文