douzhi4311 2013-07-19 14:55
浏览 17
已采纳

ZF2 - 控制/自定义单个收音机/复选框

I'm attempting to add data- values on radio buttons within ZF2. Is it possible to control each of the inputs specified with value_options?

A typical radio button added to a form:

$this->add(array(
    'type'  => 'radio',
    'name'  => 'duration',
    'options' => array(
        'value_options' => array(
            'daily' => 'Daily',
            'weekly' => 'Weekly',
            'monthly' => 'Monthly',
        ),
    ),
));  

Ultimately, I would like something like the following, so I can specify individual parameters/options for each radio item:

$this->add(array(
    'type'  => 'radio',
    'name'  => 'duration',
    'options' => array(
        'value_options' => array(
            array(
                'attributes' => array(
                    'value' => 'daily',
                    'data-item' => 'apple'
                ),
                'options' => array(
                    'label' => 'Daily'
                )
            ),
            array(
                'attributes' => array(
                    'value' => 'weekly',
                    'data-item' => 'orange'
                ),
                'options' => array(
                    'label' => 'Weekly'
                )
            ),
            array(
                'attributes' => array(
                    'value' => 'monthly',
                    'data-item' => 'pear'
                ),
                'options' => array(
                    'label' => 'Monthly'
                )
            ),
        ),
    ),
));

My reason for wanting the above, is that I want to use JavaScript to change something upon selecting a radio button, so it needs to hold data attributes.

Is anything like this possible yet?

  • 写回答

1条回答 默认 最新

  • doushenxu7294 2013-07-19 15:23
    关注

    It can be done by providing an array (or an object which implements ArrayAccess) instead of a single value (almost as you wrote in your example).

    $this->add(array(
        'type'  => 'radio',
        'name'  => 'duration',
        'options' => array(
            'value_options' => array(
                'daily' => array(
                    'label' => 'Daily',
                    'value' => 'daily',
                    'attributes' => array(
                        'data-item' => 'apple',
                    ),
                ),
                'weekly' => array(
                    'label' => 'Weekly',
                    'value' => 'weekly',
                    'attributes' => array(
                        'data-item' => 'orange',
                    ),
                ),
                'monthly' => array(
                    'label' => 'Monthly',
                    'value' => 'monthly',
                    'attributes' => array(
                        'data-item' => 'pear',
                    ),
                ),
            ),
        ),
    ));
    

    https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L177

    This should work on radios, multi-checkboxes & selects too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页