我正在尝试使用具有 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 选项。如果有人知道为什么这将非常有帮助。
谢谢