Drupal7 多个选择框使用 Ajax 回调



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

     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 回调并设置了版本元素字段。但是版本元素不会执行 ajaxcall 来设置 arch select 框,即使设置了 #ajax 数组也是如此。当我查看ajax_server_version_callback的 json 返回时,它不包含 ajax 选项。如果有人知道为什么它会非常有帮助。

感谢

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

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

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

它对我有用。

相关内容

  • 没有找到相关文章

最新更新