0

我正在尝试使用具有 4 个元素(名称、服务器、版本、操作系统、架构)的 drupal 7 创建一个表单

     function create_server(){
            $form['new_server']['name'] = array(
                '#type' => 'textfield',
                '#title' => t('Name'),
                '#required' => TRUE,
                '#description' => t('Please make sure that your server\'s name is unique.')
            );
            $form['new_server']['os'] = array(
                    '#type' => 'select',
                    '#title' => t('OS'),
                    '#options' => $os,
                    '#multiple' => FALSE,
                    '#required' => TRUE,
                    '#prefix' => '<div id="os-div">',
                    '#suffix' => '</div>',
                    '#ajax' => array(
                        'callback' => 'ajax_server_version_callback',
                        'wrapper' => 'version-div',
                        'method' => 'replace',
                        'effect' => 'fade',
                    ),
                );
    $form['new_server']['version'] = array(
                    '#type' => 'select',
                    '#title' => t('Version'),
                    '#disabled' => TRUE,
                    '#options' => array(),
                    '#ajax' => array(
                        'callback' => 'ajax_server_arch_callback',
                        'wrapper' => 'arch-div',
                        'method' => 'replace',
                        'effect' => 'fade',
                    ),
                    '#required' => TRUE,
                    '#prefix' => '<div id="version-div">',
                    '#suffix' => '</div>',
                );
                $form['new_server']['os_arch'] = array(
                    '#type' => 'select',
                   // '#disabled' => TRUE,
                    '#title' => t('OS_ARCH'),
                    '#options' => array(
                        'x86' => 'x86',
                        'x86_64' => 'x86_64'
                    ),
                    '#multiple' => FALSE,
                    '#required' => TRUE,
                    '#prefix' => '<div id="arch-div">',
                    '#suffix' => '</div>',
                );
return $form;
        }

/**
     * Ajax call for version select form
     * based on the selection.
     */

    function  ajax_server_version_callback($form, &$form_state) {

        $os =  !empty($form_state['values']['os']) ? $form_state['values']['os'] : 'all';
        $catalog = server_get_otib_content();

        if(isset($catalog['version'][$os])){
            $version = $catalog['version'][$os];
        } else {
            $version = $catalog['version'];
        }

        $form['version'] = array(
            '#type' => 'select',
             '#id' => 'edit-version',
            '#title' => t('Version'),
            '#disabled' => TRUE,
            '#options' => $version,
            '#ajax' => array(
                'callback' => 'ajax_server_arch_callback',
                'wrapper' => 'arch-div',
                'method' => 'replace',
                'effect' => 'fade',
            ),
            '#required' => TRUE,
            '#prefix' => '<div id="version-div">',
            '#suffix' => '</div>',
        );


        /* echo '<pre>';
        var_dump($form['version']);
        echo '</pre>'; */
        return $form['version'];

    }

    /**
     *  Ajax call for arch select form
     * @param array $form
     * @param array $form_state
     * @return array
     */
    function ajax_server_arch_callback($form, &$form_state) {
        $os =  !empty($form_state['values']['os']) ? $form_state['values']['os'] : 'all';
        $version =  !empty($form_state['values']['version']) ? $form_state['values']['version'] : 'all';
        $catalog = server_get_otib_content();

        if(isset($catalog['arch'][$os][$version])){
            $version = $catalog['arch'][$os][$version];
        } else {
            $version = $catalog['arch'];
        }

        $form['new_server']['os_arch'] = array(
            '#type' => 'select',
            '#disabled' => TRUE,
            '#title' => t('OS_ARCH'),
            '#options' => array(
                'x86' => 'x86',
                'x86_64' => 'x86_64'
            ),
            '#multiple' => FALSE,
            '#required' => TRUE,
            '#prefix' => '<div id="arch-div">',
            '#suffix' => '</div>',
        );
        return $form['new_server']['os_arch'];
    }

问题是当我选择第一个元素服务器时,它正确地进行了 ajax 回调并设置了版本元素字段。但是,即使设置了#ajax 数组,version 元素也不会执行 ajaxcall 来设置拱形选择框。当我查看 ajax_server_version_callback 的 json 返回时,它不包含 ajax 选项。如果有人知道为什么这将非常有帮助。

谢谢

4

1 回答 1

0

我有同样的问题,无法谷歌解决方案。我意识到的一件事是,如果元素被 ajax 替换并且具有“禁用”属性,则 ajax 回调函数不起作用。所以你可以尝试这样的事情:

  • 删除 ('#disabled' => TRUE,) 属性
  • 而是添加 some.js 文件,该文件通过 js 代码禁用元素

    (function($)  {
      $(document).ready(function(){
        $('#element').attr('disabled', 'disabled');
      });
    })(jQuery);
    

这个对我有用。

于 2013-09-24T12:35:53.720 回答